Set

The Set type is a generic collection that is similar to the array type. While the array type is an ordered collection that may contain duplicate items, the Set type is an unordered collection where each item must be unique.

Similar to the key in a dictionary, the type stored in an array must conform to the Hashable protocol. This means that the type must provide a way to compute a hash value for itself. All of Swift's basic types, such as String, Double, Int and Bool, conform to the Hashable protocol and can be used in a set by default.

Let's look at how we would use the Set type.

Initializing a set

There are a couple of ways in which we can initialize a set. Just like the array and dictionary types, Swift needs to know what type of data is going ...

Get Mastering Swift 2 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.