Intersection of Arrays
Given two arrays nums1 and nums2, return an array containing 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.
Input
nums1: an array of integersnums2: an array of integers
Output
An array of integers representing the intersection of the two arrays, preserving duplicate counts
Examples
Example 1:
Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2]
Output: [2, 2]
Explanation: The number 2 appears twice in both arrays, so it appears twice in the result.
Example 2:
Input: nums1 = [4, 9, 5], nums2 = [9, 4, 9, 8, 4]
Output: [4, 9] or [9, 4]
Explanation: Each number (4 and 9) appears at least once in both arrays. The output order does not matter.
Example 3:
Input: nums1 = [1, 2, 3], nums2 = [4, 5, 6]
Output: []
Explanation: The arrays have no common elements.