Skip to main content

Command Palette

Search for a command to run...

Find the Difference of Two Arrays

Published
2 min read
Find the Difference of Two Arrays
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.

  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

LeetCode Problem - 2215

class Solution {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
        // Initialize the answer list which will hold two lists
        List<List<Integer>> answer = new ArrayList<>();

        // Initialize a temporary list to hold elements unique to nums1
        List<Integer> temp = new ArrayList<>();

        // Iterate over each element in nums1
        for(int e : nums1) {
            // Check if the element is not present in nums2
            if(!(IntStream.of(nums2).anyMatch(x -> x == e))) {
                // If the element is not already in temp, add it
                if(!temp.contains(e)) {
                    temp.add(e);
                }
            }
        }
        // Add the list of unique elements from nums1 to the answer list
        answer.add(temp);

        // Initialize a temporary list to hold elements unique to nums2
        List<Integer> temp1 = new ArrayList<>();

        // Iterate over each element in nums2
        for(int e : nums2) {
            // Check if the element is not present in nums1
            if(!(IntStream.of(nums1).anyMatch(x -> x == e))) {
                // If the element is not already in temp1, add it
                if(!temp1.contains(e)) {
                    temp1.add(e);
                }
            }
        }
        // Add the list of unique elements from nums2 to the answer list
        answer.add(temp1);

        // Return the final answer list containing the unique elements from both arrays
        return answer;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.