Return the unique values that appear in one array but not the other, in both directions.
Problem
You are given two integer arrays nums1 and nums2. Return the difference between the two arrays as two lists:
- The first list contains the distinct values that appear in
nums1but do not appear innums2. - The second list contains the distinct values that appear in
nums2but do not appear innums1.
The order of values inside each list does not matter.
Goal
Compute the two set differences efficiently by identifying which values are present in each array and filtering out common values.
Input Format
nums1: an array of integersnums2: an array of integers
Output Format
Return a list of two lists:
answer[0]: distinct integers innums1but not innums2answer[1]: distinct integers innums2but not innums1
Constraints
1 <= nums1.length, nums2.length <= 1000-1000 <= nums1[i], nums2[i] <= 1000- Output values must be distinct within each list
- Any order is acceptable
Example 1
Input
nums1 = [1,2,3], nums2 = [2,4,6]
Output
[[1,3],[4,6]]
Explanation
Values 1 and 3 are only in the first array, while 4 and 6 are only in the second array.
Example 2
Input
nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output
[[3],[]]
Explanation
After removing duplicates, only 3 is exclusive to nums1; every value in nums2 also appears in nums1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.