Intersection of Two Arrays II

Given two integer arrays, return their intersection including duplicates. Sorting both arrays makes a two-pointer intersection pass straightforward.

Input Format

nums1 = first array, nums2 = second array

Output Format

intersection array including duplicates

Constraints

  • 0 <= nums1.length, nums2.length <= 10^5; -10^9 <= nums1[i], nums2[i] <= 10^9

Examples

Example 1:

Input:

nums1 = [1,2,2,1]
nums2 = [2,2]

Output:

[2,2]

Explanation:

Both arrays share two copies of 2.

Example 2:

Input:

nums1 = [4,9,5]
nums2 = [9,4,9,8,4]

Output:

[4,9]

Explanation:

The common elements are 4 and 9, each once.

Loading...
Intersection of Two Arrays II