Given two arrays, write a function to compute their intersection.
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Thoughts: Took a bit longer to understand what the question means by ‘as many times’. Use
Map() is the way. First, to build a map and record occurrences of each unique elements, then iterate over
nums2 and add elements to
res if the value of element in Map is no less than