LeetCode: Intersection of Two Arrays Solution

Set intersection

Approach

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 = []
4
5 for (const num of set1) {
6 if (set2.has(num)) {
7 res.push(num)
8 }
9 }
10
11 return res
12}

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.

Next Post

LeetCode: Maximum Product of Three Numbers

Sort and keep in mind of negative numbers

HoningJS

Search Posts