LeetCode: Intersection of Two Arrays Solution
Set intersectionApproach
Convert 2 array to set and find intersection
Implementation
1var intersection = function (nums1, nums2) {2 const [set1, set2] = [new Set(nums1), new Set(nums2)]3 const res = []45 for (const num of set1) {6 if (set2.has(num)) {7 res.push(num)8 }9 }1011 return res12}
Comments
Loading comments...
Tags
leetcode
array
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.