r/learnjavascript • • May 14 '21

Remove Duplicate Value from array

Post image
234 Upvotes

89 comments sorted by

View all comments

20

u/slowthedataleak May 14 '21

Does this perform better than filter()?

2

u/47milliondollars May 14 '21

Would filter even work? Or do you have to use reduce to check if the value exists yet in an accumulator?

7

u/space_scarab May 14 '21
arr.filter((el, index) => {
    return arr.indexOf(el) === index;
});

11

u/47milliondollars May 14 '21

Oh holy shit, never thought about this. Looks like the indexOf is finding the first match, and so the filter drops anything of the same value that isn't at the first detected index? Pretty neat.