DEV Community

loading...

Discussion on: How to remove duplicates from JavaScript array

Collapse
mahmoudibnsamy profile image
Mahmoud Ibn Samy

Great ways but is it efficient to use the sets method? Especially in the big arrays.

Collapse
andrewbridge profile image
Andrew Bridge

I'd imagine if you were using a Set from the start, and adding values via the .add method on the Set object, you wouldn't need to store or iterate through the all the duplicates. That won't help if you needed to retain all the dupe data for some other uses though.

Collapse
mahmoudibnsamy profile image
Mahmoud Ibn Samy

I agree that in some use cases you can't use Set. Actually, in some cases, you can't even use it from the beginning, As an example, if you want to have a collection of unique objects, Set won't help.

Collapse
hi_iam_chris profile image
Kristijan Pajtasev Author

There is a topic on the StackOverflow showing the set is faster. I do personally like filter more, but in most cases, you won't see the difference, and you can go for what looks best in your code.

Collapse
mahmoudibnsamy profile image
Mahmoud Ibn Samy

That's good. Thanks.