#349

Intersection of Two Arrays

Easy HashSet

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the distinct values present in both arrays.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: distinct values present in both arrays.

Why this works: put one array in a set; remove a value when found so it's reported once.

Example: [1, 2, 2, 1] and [2, 2]

set {1, 2}2 found → remove → [2]; next 2 not in set

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Returning duplicates when a common value repeats in nums2.

Solve on LeetCode ↗
Complexity
  • Time: O(n + m).
  • Space: O(n).
Topic: Hashing
🎬 Episodes 37–39: video coming soon