For an array of objects ["a","b","a","c","d","b"]
I'd like to get an array of the duplicates:
["a","b"]
.
Is there a way to do this efficiently, similar to set
([...new Set(myArray)];
)?
For an array of objects ["a","b","a","c","d","b"]
I'd like to get an array of the duplicates:
["a","b"]
.
Is there a way to do this efficiently, similar to set
([...new Set(myArray)];
)?
You could still use a Set
and filter the array by checking the existence.
const
items = ["a", "b", "a", "c", "d", "b"],
duplicates = items.filter((s => v => s.has(v) || !s.add(v))(new Set));
console.log(duplicates);