Array Intersection - Problem
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.
Note: You cannot use built-in set operations or hash maps for this problem.
Input & Output
Example 1 — Basic Intersection
$
Input:
nums1 = [1,2,2,1], nums2 = [2,2]
›
Output:
[2]
💡 Note:
The only common element between both arrays is 2. Even though 2 appears multiple times, we return it only once.
Example 2 — Multiple Common Elements
$
Input:
nums1 = [4,9,5], nums2 = [9,4,9,8,4]
›
Output:
[4,9]
💡 Note:
Both 4 and 9 appear in both arrays. The result can be in any order.
Example 3 — No Intersection
$
Input:
nums1 = [1,2,3], nums2 = [4,5,6]
›
Output:
[]
💡 Note:
No elements are common between the two arrays, so return empty array.
Constraints
- 1 ≤ nums1.length, nums2.length ≤ 1000
- 0 ≤ nums1[i], nums2[i] ≤ 1000
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code