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, and answer[1] is a list of all distinct integers in nums2 which are not present in nums1. The integers in each list may be returned in any order.

30:00

Find the Difference of Two Arrays
easy
Free written solution
Topics
Companies

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, and answer[1] is a list of all distinct integers in nums2 which are not present in nums1. The integers in each list may be returned in any order.

Example 1:
Input: {"nums1":[1,2,3],"nums2":[2,4,6]}
Output: [[1,3],[4,6]]
Constraints:
  • 1≤nums1.length,nums2.length≤10001 \leq \text{nums1.length}, \text{nums2.length} \leq 1000

  • −1000≤nums1[i],nums2[i]≤1000-1000 \leq \text{nums1}[i], \text{nums2}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums1":[1,2,3],"nums2":[2,4,6]}

Add nums1[0] = 1 to set1. Sets keep one copy of each value and answer "is x here?" in O(1).

nums1

1
2
3

nums2

2
4
6

hash sets (duplicates collapse)

set1 (1)

1

set2 (0)

empty

Building the sets...

venn: answer[0] · shared · answer[1]

only nums1 → answer[0]

in both (dropped)

only nums2 → answer[1]

Variables
VariableValue
i0
x1
len(set1)1
DepthFunction Call
Stack empty
0/12