Create a difference function that takes two arrays as arguments and returns an array containing the elements that are present in the first array but not in the second array. The function should avoid redundant operations on duplicate values.
console.log(difference([], [])); // []
console.log(difference([1, 1, 2, 3], [2, 3])); // [1, 1]
console.log(difference([1, 2, 3], [1, 2, 3, 4])); // []
console.log(difference([4, 3, 2, 1], [1, 2, 3])); // [4]
function difference(arr1,arr2){
const resultArr =[]
for(item of arr1){
if(arr2.indexOf(item) < 0){
resultArr.push(item)
}
}
return resultArr
}
function difference(arr1,arr2){
const set = new Set(arr2)
return arr1.filter((item)=>{return !set.has(item)})
}