Find the Difference of Two Arrays - Problem
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 innums1which are not present innums2.answer[1]is a list of all distinct integers innums2which are not present innums1.
Note that the integers in the lists may be returned in any order.
Input & Output
Example 1 — Basic Case
$
Input:
nums1 = [1,2,3], nums2 = [2,4,6]
›
Output:
[[1,3],[4,6]]
💡 Note:
Elements 1 and 3 are in nums1 but not nums2. Elements 4 and 6 are in nums2 but not nums1.
Example 2 — With Duplicates
$
Input:
nums1 = [1,2,3,3], nums2 = [1,1,2,2]
›
Output:
[[3],[]]
💡 Note:
Element 3 is in nums1 but not nums2. All elements of nums2 exist in nums1, so second array is empty.
Example 3 — No Common Elements
$
Input:
nums1 = [1,2], nums2 = [3,4]
›
Output:
[[1,2],[3,4]]
💡 Note:
No elements are common between arrays, so each array contains all its unique elements.
Constraints
- 1 ≤ nums1.length, nums2.length ≤ 1000
- -1000 ≤ nums1[i], nums2[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
nums1 = [1,2,3] and nums2 = [2,4,6]
2
Find Differences
Elements in nums1 not in nums2: [1,3]. Elements in nums2 not in nums1: [4,6]
3
Output
Return [[1,3], [4,6]] as the answer
Key Takeaway
🎯 Key Insight: Use hash sets to convert O(n×m) linear searches into O(1) lookups
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code