Mobile devs get a bad rap when it comes to data structures & algorithms.
Unless you’re talking about animation jitters, complex optimisation problems rarely filter down to us on the client-side.
With ARM chips chugging along like a Firebolt at 2 teraflops, and when we’re dealing with a few dozen paged elements on-screen at once, the difference between O(n) and O(log(n)) rarely matters to us.
The Set is like the Rupert Grint of data structures. Less star power than the Array (Daniel Radcliffe); and less sheer brilliance than the Dictionary (Emma Watson); but really unmatched in owning the role it’s given.
To help you build up your intuition for when to use this red tetrahedral peg, I’ll talk you through some real-world scenarios when I encountered triangular holes.
Improving the UX of API polling with partial results
Recently, I was working on a feature that involved a long-running job on our backend. A user would sign up, and it might take us 10 minutes to process all the data we needed to show them.
While we were processing, we wanted to give the user some feedback, so we settled on a polling system where every 10 seconds, we returned partial results to the user.
The API we had used pagination, so the first-page results displayed after the first 10 seconds could be completely different list to the results after 1 minute or longer.
To ensure the experience wasn’t confusing, we need to:
combine partial results with existing results
remove duplicate items
maintain the ordering
Removing duplicates
Casting an Array of values into a Set instantly casts a vanishing spell on those pesky duplicates. Simply cast the incantation Array(Set(data)) to transfigure the result back into an Array.

Since Sets are unordered, you can’t guarantee the order of the resulting Array will be the same as you started with - conform your data type to Comparable and sort it to keep the ordering right.
How can I XOR my custom data structures?
I used to think Set algebra was as fun as a date with Umbridge, but I didn’t truly appreciate the power I was playing with.
symmetricDifference is a property unique to Sets which allows you to rapidly find out which items exist in one Set, but do not exist in another Set.
In a Venn diagram, it’s everything in circle A that does not overlap with B - equivalent to a XOR operation. Because I’m terrified of getting sent to Azkaban for copyright violation, here is a diagram I drew myself in MS Paint:
Purging the keychain on logout
In your app, you might be storing some important pieces of data on the iOS keychain.
Some of this data is important to keep on-device between login sessions (e.g. a unique download token to track a device in analytics). Most, however, should be erased if a user logs out (e.g. auth tokens or your user’s email address).
We could maintain a list of keychain items to specifically remove on log-out, but in a complex app you might have 50+ items in the keychain. More likely, the list of items to keep is far smaller.
Rather than maintaining a long list of items to purge on logout, we can use symmetricDifference to maintain a much smaller list of items to persist on logout, and purge everything else

symmetricDifference, an operation someone on Twitter once didn’t believe me when I said I’d used 


