Chapter 11. Sets

Swift provides a third collection type called Set. Set is not frequently used, but we do not think that this should be the case. This chapter will introduce Set and show off some of its unique advantages.

What Is a Set?

A Set is an unordered collection of distinct instances. This definition sets it apart from an Array, which is ordered and can accommodate repeated values.

A Set has some similarities to a Dictionary, but it is also a little different. Like a dictionary, a set’s values are unordered within the collection. Also, like a dictionary’s keys, the values in a Set must be unique; a value can only be added to a set once. To ensure that elements are unique, Set requires that its elements follow the same rule as a ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.