Sets, The Forgotten Collection Type

February 26, 2016

How have I been programming for almost two years and never explored the power of sets? It seems that sets are perhaps the forgotten child of data collection or I'm just not the developer I thought I was? Either way, I was recently reading the Swift Apprentice made by the amazing team over on and realized I'd been missing a piece of the puzzle. Today I'll get into what I learned about sets and a few practical uses for them that I've found.

We can start by defining sets as a collection of distinct values of the same type and with no defined order. In layman's terms you'll have a collection just like the other collection types but with no duplicate values and they're not ordered in any way. You'll also see that sets have some powerful features when comparing two set collections. First we'll start by declaring and initializing a set.

The good thing about the syntax is it's very clear when you've just made a set.

If you're like me you probably just wanna know why should you use a set. Sets have some great built-in features that may fit your problem just perfectly.

Lets say you're building an app that allows party host to make a list of things they'd like guest to bring.

When a guest says they can bring a certain item you simply check if it's still on the list and remove it.

You could shorten that down to a single line of code by using the subtract method which removes values that also appear in the second set.

The above code is a great example of handy methods you can use with sets. I'll show you a few more.

For the next example lets say you and your co-host are making a list of friends to invite to the party and for some unknown reason you're not together making these list. You could simply check each others list and remove the duplicates but that defeats the point of technology. Instead your handy set list methods can take care of that. The union operation will simply make a new set from the combination of the ones provided. Remember, sets only contain Unique values meaning that no duplicates will be in the new set if there are any.

Here you've got a new list of friends you both helped make and no duplicates! Set list are magic I know!

Alternatively, you could say only friends you both agree on can show up to the party and in that case set list have you covered as well. We'll use the intersect operation to test both sets and create a new one that only has the values both sets have in common.

Finally you and your co-host have to pick a time for the party. You each decide the best way to decide is to put down the times you are busy and then pick from the times you both are free. One of Sets powerful operations is exclusiveOr. This method looks at two sets and takes the values that are only contained in one not both.

Those are the main highlights of what you'll get with sets. They're powerful and when you want the free functionality of filtering and removing duplicates they're perfect. To help you work it into your current work flow you should know it's easy to convert a set into an array.

You can save a lot of time by simply using sets and as you can see they're easy to convert back into a friendly array. Honestly, I almost always use the sort function to get an array back. It's just my preferred way of storing data but you are free to do either now!

Thanks for reading! Make sure to use sets in playground and checkout all their cool functionality. If you feel like you got something out of reading this please comment and let me know! I really enjoy knowing I provided some benefit to my readers and I hope you'll share it so others can too.