ds-algo-study
Version:
Just experimenting with publishing a package
28 lines (20 loc) • 778 B
JavaScript
/******************************************************************************
Write a function intersect(arr1, arr2) that takes in two arrays and returns a
new array containing the elements common to both arr1 and arr2.
Hint: use indexOf
Examples:
intersect(['a', 'b', 'c', 'd'], ['b', 'd', 'e']) => [ 'b', 'd' ]
intersect(['a', 'b', 'c'], ['x', 'y', 'z']) => []
*******************************************************************************/
function intersect(arr1, arr2) {
let commonEles = [];
for (let i = 0; i < arr1.length; i++) {
let ele = arr1[i];
if (arr2.indexOf(ele) > -1) {
commonEles.push(ele);
}
}
return commonEles;
}
/**************DO NOT MODIFY ANYTHING UNDER THIS LINE*************************/
module.exports = intersect;