Intersection of Two Arrays
Given two integer arrays nums1 and nums2, return their intersection.
Each element in the result must be unique, even if it appears multiple times in both arrays.
Return the result in ascending order.
Example 1
Input
nums1 = [1, 2, 2, 1] nums2 = [2, 2]
Output
[2]
Explanation
The element 2 appears in both arrays. Duplicate occurrences are included only once.
Example 2
Input
nums1 = [4, 9, 5] nums2 = [9, 4, 9, 8, 4]
Output
[4, 9]
Explanation
The common elements are 4 and 9. The result is sorted in ascending order.
Constraints
Hints:
Hint 1
Store the elements of one array in a HashSet for efficient lookup.
Hint 2
Traverse the second array and collect elements that exist in the first array. Use another set to avoid duplicates, then sort the result.
Author & Technical Reviewer
Published
Nov 14, 2026
Last updated
Nov 14, 2026
Last technically reviewed
Nov 14, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output