[Easy] 349. Intersection of Two Arrays
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Examples
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Explanation: The intersection contains only the element 2.
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4] or [4,9]
Explanation: The intersection contains elements 9 and 4. Order does not matter.
Constraints
1 <= nums1.length, nums2.length <= 10000 <= nums1[i], nums2[i] <= 1000
Thinking Process
- Hash Set for Uniqueness: Automatically handles duplicates
- Two indices move toward each other or in the same direction.
- Works on sorted arrays or when in-place modification is required.
- Loop invariant: all indices outside
[left, right]are already resolved.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Opposite ends (this problem) | O(n) | O(1) | Sorted array pair search, reversal |
| Slow / fast pointers | O(n) | O(1) | Linked list middle, cycle detection |
| Same-direction chase | O(n) | O(1) | Remove duplicates in-place |
| Sliding window (variable) | O(n) | O(1) | Subarray with constraint |
Solution
class Solution:
def intersection(self, nums1, nums2):
seen = set(nums1)
rtn = []
for num in nums2:
if num in seen:
rtn.append(num)
seen.remove(num)
return rtn
Solution Explanation
Approach: Opposite ends (this problem)
Key idea: 1. Hash Set for Uniqueness: Automatically handles duplicates
How the code works:
- Hash Set for Uniqueness: Automatically handles duplicates
- Two indices move toward each other or in the same direction.
- Works on sorted arrays or when in-place modification is required.
- Loop invariant: all indices outside
[left, right]are already resolved.
Walkthrough — input nums1 = [1,2,2,1], nums2 = [2,2], expected output [2]:
The intersection contains only the element 2.
Common Mistakes
- No intersection:
nums1 = [1,2,3],nums2 = [4,5,6]→[] - Complete overlap:
nums1 = [1,2,3],nums2 = [1,2,3]→[1,2,3] - One array empty:
nums1 = [],nums2 = [1,2]→[] - Duplicates in both:
nums1 = [1,1,2,2],nums2 = [2,2]→[2] -
Single element:
nums1 = [1],nums2 = [1]→[1] - Not removing from set: Results in duplicate elements in output
- Using vector instead of set: Doesn’t handle duplicates in
nums1 - Wrong comparison: Comparing entire arrays instead of elements
- Forgetting empty check: Not handling empty arrays
- Order dependency: Trying to maintain order when not needed
Related Problems
- LC 350: Intersection of Two Arrays II - Allow duplicates in result
- LC 349: Intersection of Two Arrays - Unique elements only (this problem)
- LC 1002: Find Common Characters - Find common characters in strings
- LC 1213: Intersection of Three Sorted Arrays - Three arrays intersection
- LC 2248: Intersection of Multiple Arrays - Multiple arrays intersection
Key Takeaways
- Hash Set for Uniqueness: Automatically handles duplicates
- Erase After Add: Prevents duplicate results efficiently
- Order Independence: Can return result in any order
- Space-Time Trade-off: Hash set uses more space but provides O(1) lookup
References
- LC 349: Intersection of Two Arrays on LeetCode
- LeetCode Discuss — LC 349: Intersection of Two Arrays
- LeetCode Editorial (may require premium)