12. Intersection of Two Arrays

EasyArrayArray

Given two integer arrays nums1 and nums2, return an array of their intersection.

Each element in the result must be unique. The result can be in any order.

Input: Two integer arrays nums1 and nums2.

Output: A sorted array of unique elements present in both arrays.

Examples

Example 1
Input: [1,2,2,1],[2,2]
Output: [2]
Explanation: Common elements: {2}. Even though 2 repeats, output contains it once. Return [2].
Example 2
Input: [4,9,5],[9,4,9,8,4]
Output: [4,9]
Explanation: Common distinct elements: {4,9}. Return [4,9].
Example 3
Input: [1,2,3],[4,5,6]
Output: []
Explanation: No common elements. Return [].

Constraints

Asked by

AccentureBloombergCapgeminiAmazonGoogleMicrosoft
Solve this problem in the editor →