LeetCode: Intersection Of Two Arrays II Solution

1var intersect = function (nums1, nums2) {
2 const getElOccMap = arr =>
3 arr.reduce((acc, el) => acc.set(el, (acc.get(el) || 0) + 1), new Map())
4 const map1 = getElOccMap(nums1)
5 const map2 = getElOccMap(nums2)
6 const res = []
7 for (const [el, occ] of map1) {
8 if (map2.has(el)) {
9 res.push(Array(Math.min(occ, map2.get(el))).fill(el))
10 }
11 }
12 return res.flat()


Loading comments...



hash table

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Contains Duplicate

Feb 4, 2021



Search Posts