Intersection of Two Arrays II

Easy

Description

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays. The result can be in any order.

Examples

Input:nums1 = [1,2,2,1], nums2 = [2,2]
Output:[2,2]
Explanation:

2 appears twice in both.

Input:nums1 = [3,1,3,3,4], nums2 = [3,3,1,1,5]
Output:[3,3,1]
Explanation:

3 appears 3 times in nums1 and 2 times in nums2, so min(3,2)=2 occurrences are taken. 1 appears 1 time in nums1 and 2 times in nums2, so min(1,2)=1 occurrence is taken. 4 and 5 do not appear in both arrays.

Input:nums1 = [7,8], nums2 = [6,9,10]
Output:[]
Explanation:

No elements appear in both arrays, so the intersection is empty. This demonstrates the case when arrays have no common elements.

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!