Intersection of Two Arrays

Easy~15 min

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

For example, if nums1 has two 2s and nums2 has three 2s, the result should contain two 2s (the minimum count).

Examples

Example 1
Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2]
Output: [2, 2]
Explanation: 2 appears twice in nums1 and twice in nums2, so the intersection contains two 2s.
Example 2
Input: nums1 = [4, 9, 5], nums2 = [9, 4, 9, 8, 4]
Output: [4, 9]
Explanation: 4 appears once in both, 9 appears once in nums1 and twice in nums2, so the intersection has one 4 and one 9.
Example 3
Input: nums1 = [1, 2, 3], nums2 = [4, 5, 6]
Output: []
Explanation: No elements in common.

Constraints

  • 0 <= nums1.length, nums2.length <= 10^4
  • -10^5 <= nums1[i], nums2[i] <= 10^5
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run