Notes & software courses · Free to learn
Aph's Blog
On this page

2215. Find the Difference of Two Arrays

👋 อ่านฟรีทั้งหมดบน Aph's Blog — เนื้อหาภาษาไทย ทำตามทีละหน้าใน sidebar ได้เลย หากมีข้อเสนอแนะหรืออยากให้เพิ่มหัวข้อไหน บอกได้เสมอ

Find values in one array but not the other with set difference.

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 nums1. 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

Understand the problem

The problem talks about unique values and “in this set but not that set,” which matches set semantics exactly: sets drop duplicates automatically and support a difference operator.

If you skip sets and check with if x in nums2 on a list, each check is O(n) and the whole solution becomes O(n²). Converting to sets first is worth it because membership checks become O(1).

Approach

  1. Convert nums1 to set s1 and nums2 to set s2 (dedupes for free)
  2. Compute s1 - s2 = values in s1 but not in s2
  3. Compute s2 - s1 = values in s2 but not in s1
  4. Return a two-element list, converting each set back to a list
Common pitfalls

Don’t forget difference in both directions (s1-s2 and s2-s1) — they are different. And because order does not matter, you don’t need to worry about how list(set(...)) is ordered.

Walkthrough — nums1 = [1, 2, 3, 3], nums2 = [1, 1, 2, 2]

  1. Convert to sets: s1 = {1, 2, 3} (duplicate 3 collapses), s2 = {1, 2}
  2. s1 − s2 = {1, 2, 3} − {1, 2} = {3}
  3. s2 − s1 = {1, 2} − {1, 2, 3} = ∅ (empty)
  4. Convert back to lists → [[3], []]

Edge case: identical arrays like nums1 = [1, 2], nums2 = [1, 2] yield empty sets both ways → [[], []], which is correct. Negative numbers work fine in a Python set.

Try it yourself first

Approach and walkthrough are above — write it yourself, then open the fold below when stuck or ready to compare.

Solution code · folded so you can try firstพับไว้ด้านใน — คลิกเมื่อพร้อมดู

Core: convert to sets, then difference both ways — uniqueness and membership in one line.

Python — runnablepython
def find_difference(nums1, nums2):
    s1, s2 = set(nums1), set(nums2)  # dedupe each array
    # s1 - s2 = in s1 but not in s2
    # s2 - s1 = in s2 but not in s1
    return [list(s1 - s2), list(s2 - s1)]

print(find_difference([1, 2, 3], [2, 4, 6]))        # [[1, 3], [4, 6]]
print(find_difference([1, 2, 3, 3], [1, 1, 2, 2]))  # [[3], []]
Output
[[1, 3], [4, 6]]
[[3], []]

What to notice

  • set(...) drops duplicates and makes membership O(1)
  • s1 - s2 and s2 - s1 are different — do both
  • list(...) converts back because the judge wants arrays; order does not matter
  • Staying on lists without sets is O(n²) and you must dedupe yourself
Time · Space

Time O(n + m) build two sets + differences · Space O(n + m) for the two sets

💡 Pattern takeaway

When a problem talks about “in this group but not that group” or “values that differ,” think set difference immediately. The set operators & | - keep set logic short and fast.