Intersection of arrays
Standard · Write code · about 10 min
Given two integer arrays nums1 and nums2, return the values that appear in both. Each value appears once in the result, in any order.
Example 1
Input:
[[1,2,2,1],[2,2]]
Output:
[2]
2 appears in both arrays, and it is listed once.
Example 2
Input:
[[4,9,5],[9,4,9,8,4]]
Output:
[4,9]
The order of the result does not matter.
Constraints
- 0 ≤ nums1.length, nums2.length ≤ 10,000
- -1,000,000 ≤ value ≤ 1,000,000
Learn the idea first: Hash sets and maps in practice: counting, lookup, deduplication, a visual lesson.
More Hash sets and maps in practice: counting, lookup, deduplication problems
- Contains duplicate Warm-up
- Build a frequency map Warm-up
- Two Sum Standard
- First unique character Standard
- Group anagrams Standard
- Longest consecutive sequence Challenge