I write a function that receives 2 arrays and returns an array that has elements that exist in both arrays. For example, if I pass [6,7,8,9] and [1,8,2,6], it should return [6,8].
My aim is not to use loops here.
I use this code:
const uniqueElements= (arr1, arr2) => {
return arr1.filter(it1=> arr2.filter((it2) => it2===it1).length>0)
}
However, if there are duplicate elements in arrays (e.g. [6,7,8,9,6] and [1,8,2,6,6]), it returns [6, 8, 6].
How should I mend my code so that it would return only unique elements without duplicates? Is it possible without using loops?