Find the Difference of Two Arrays

Leetcode Daily Challenge (3rd May, 2023)

Find the Difference of Two Arrays

Problem Statement:-

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.

Link: https://leetcode.com/problems/find-the-difference-of-two-arrays/description/

Problem Explanation with examples:-

Example 1

Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].

Example 2

Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output: [[3],[]]
Explanation:
For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
Every integer in nums2 is present in nums1. Therefore, answer[1] = [].

Constraints

  • 1 <= nums1.length, nums2.length <= 1000

  • -1000 <= nums1[i], nums2[i] <= 1000

Intuition:-

  • First method can be to create a hash map and store value 1 against each element of nums1 to indicate that the element is present in nums1.

  • Then, we can iterate over nums2 and check if the element is present in the hash map. If it is, we can change it value to 3 to indicate that the element is present in both the arrays. If it is not, we can add it to the hash map with value 2 to indicate that the element is present in nums2.

  • After the loop completes, we can iterate over the hash map and add the elements with value 2 to the answer array. These elements are present in nums2 but not in nums1. We can also add the elements with value 1 to the answer array. These elements are present in nums1 but not in nums2.

  • Return the answer array.

  • Another method can be to convert to set and find the difference between the two sets. The difference between the two sets will give us the elements that are present in one set but not in the other.

Solution:-

  • Initialize a hash map.

  • Iterate over nums1. Add each element to the hash map with value 1.

  • Iterate over nums2. If the current element is present in the hash map, change its value to 3. If it is not, add it to the hash map with value 2.

  • Initialize two empty arrays, one for the elements present in nums1 but not in nums2 and the other for the elements present in nums2 but not in nums1.

  • Iterate over the hash map. If the value of the current element is 2, add it to the second array. If the value of the current element is 1, add it to the first array.

  • Return the two arrays.

  • For the second method, convert nums1 and nums2 to sets. Find the difference between the two sets. The difference between the two sets will give us the elements that are present in one set but not in the other. Put these elements in two arrays and return them.

Code:-

JAVA Solution

class Solution {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
        HashMap<Integer,Integer> mp = new HashMap<>();
        List<List<Integer>> ans = new ArrayList<>();

        for(int x:nums1){
            mp.put(x,1);
        }

        for(int x:nums2){
            if(mp.getOrDefault(x,0) == 1)
                mp.put(x,3);
            else if(mp.getOrDefault(x,0) == 0)
                mp.put(x,2);
        }

        List<Integer> f1 = new ArrayList<>();
        List<Integer> f2 = new ArrayList<>();
        for(int x:nums1){
            if(mp.getOrDefault(x,0) == 1){
                mp.put(x,0);
                f1.add(x);
            }

        }

        ans.add(f1);
        for(int x:nums2){
            if(mp.getOrDefault(x,0) == 2){
                mp.put(x,0);
                f2.add(x);
            }
        }


        ans.add(f2);

        return ans;
    }
}

Python Solution

class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        return [list(set(nums1).difference(nums2)), list(set(nums2).difference(nums1))]

Complexity Analysis:-

TIME:-

The time complexity is O(n) where n is the length of the longer array. We iterate over both the arrays once. In case of the second method, we convert the arrays to sets. This takes O(n) time as well.

SPACE:-

The space complexity is O(n) where n is the length of the longer array. We use a hash map to store the elements of the longer array. In case of the second method, we convert the arrays to sets. This takes O(n) space as well.

References:-

Connect with me:-

Did you find this article valuable?

Support Leeting-LCS by becoming a sponsor. Any amount is appreciated!