0

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)];)?

Jason
  • 7,612
  • 14
  • 77
  • 127

1 Answers1

1

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392