Question 20 of 75 - LeetCode Weekly Challenge

LeetCode Challenge #2215. Find the Difference of Two Arrays

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.

 

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
Video Solution
C++ Solution
				
					class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
        // Create unordered sets to store unique elements from nums1 and nums2
        unordered_set<int> set1(nums1.begin(), nums1.end());
        unordered_set<int> set2(nums2.begin(), nums2.end());

        // Initialize a 2D vector to store the differences (elements in nums1 not in nums2 and vice versa)
        vector<vector<int>> answer(2);

        // Iterate through unique elements in set1
        for (int num : set1) {
            // If the element is not present in set2, add it to the first vector of differences (elements in nums1 not in nums2)
            if (set2.count(num) == 0) {
                answer[0].push_back(num);
            }
        }

        // Iterate through unique elements in set2
        for (int num : set2) {

            // If the element is not present in set1, add it to the second vector of differences (elements in nums2 not in nums1)
            if (set1.count(num) == 0) {
                answer[1].push_back(num);
            }
        }

        // Return the 2D vector containing the differences
        return answer;
    }
};

				
			
Code Explanation

In this program, we have to find the difference between two sets represented by vectors (‘nums1’ and ‘nums2’). The sets are implemented using unordered sets. 

An unordered set (std::unordered_set) is a container that contains a set of unique elements. It uses hashing to achieve constant time-average complexity for insertions and deletions.

Let’s see the step-by-step approach to the solution:

  1. Let’s make two unordered sets ‘set1’ and ‘set2’ both of ‘int’ type in which they contain all the elements of ‘nums1’ and ‘nums2’ from beginning to end. 
  1. We have to make a 2D vector ( since it is given that we have to return a 2D array) ‘answer’ with two vectors inside it ‘answer[ 0 ]’  will store the elements that are in ‘nums1’ but not in ‘nums2’, and ‘answer[ 1 ] ‘ will store the elements that are in ‘nums2’ but not in ‘nums1’.
  1. Now, we will start the iteration through the elements of ‘set1’ and check if each element is not present in ‘set2’. If they are not present, add it to ‘answer[ 0 ]’. 
  • Similarly, we will iterate through the elements of  ‘set2’ and check if each element is not present in ‘set1’. If not present, add it to ‘answer[ 1]’. 
  1. Now ‘push_back’ function is used to add elements to vectors within a 2D vector (‘answer’). 

Finally, return ’ answer ‘.

Summary:- the solution used unordered sets to find and store the differences between two sets represented by vectors. The use of unordered sets ensures that the resulting differences are unique.

Happy Coding with edSlash