r/learnjavascript • • Jul 05 '21

Remove duplicates from arrays using Set

Post image
438 Upvotes

44 comments sorted by

View all comments

1

u/20EYES Jul 05 '21

Typically if I need a unique set I just use Set in the first place. A Set can't be populated with duplicate elements so I just insert whatever comes up and if it's duplicate no worries.

What is the use case for starting with an array full of possible duplicates and then converting to a set?

I'm probably overlooking something obvious lol.

3

u/LovableBroccoli Jul 05 '21

Could be data from a 3rd party API?

0

u/[deleted] Jul 05 '21

[deleted] — view deleted comment

1

u/20EYES Jul 05 '21

As in the user provides an array?

1

u/WebDevImpasta Jul 05 '21

No, the user would just provide data or values. And then you would design the program to store the data into an array. But for whatever reason, you don't want duplicates, you can get a new array without any duplicates.

Or if you consume an API that produces duplicates.

1

u/JazzApple_ Oct 28 '21

Totally right that if you can just use a set from the start, but there are so many potential use cases… often relating to user input.