To make a set you can use its constructor method:
That last set illustrates the “magic” of sets. They don’t throw errors for having duplicates, but more preferably they prevent duplicate data from being added to their contents.
Sets have several useful and straightforward methods built in, as well:
Sets also a have a few different iteration methods available to them, but several of them are redundant:
You can go one step further and incorporate sets in your functions if you want to prevent duplicates to begin with. Imagine a function meant to collect user input from an event that you only want unique inputs from:
There’s plenty of other valid ways to accomplish this kind of data filtering too, but if you’ve didn’t know about sets before or if you hadn’t considered using them, then perhaps now you can see the kinds of solutions it offers. The next time time you need unique data, give sets a try! I hope you found this helpful.