intersection

SunJet Liu
May 31, 2021

--

Write a function, intersection, that takes in two arrays, a,b, as arguments. The function should return a new array containing elements that are in both of the two arrays.

You may assume that each input array does not contain duplicate elements.

const intersection = (a, b) => {
// todo
let set = new Set()
let arr = []
for( let i = 0; i < a.length; i++){
set.add(a[i])
}
for(let j = 0; j < b.length; j ++){
if (set.has(b[j])){
arr.push(b[j])
}
}
return arr
}

--

--