Sets & Set Operations


A set stores distinct values of the same type in a collection with no defined ordering. Sets are used as an alternative to arrays when item order is not a concern or when you need to ensure that an item appears only once.
For a Swift set, write the type as Set<T> where T is the type that the set is allowed to store. Unlike arrays, there is no equivalent shorthand for sets.
You can create an empty set of a certain type using initializer syntax:

var letters = Set<Character>()

Based on the initializer type, Swift infers the type of the letters to be Set<Character>.
An array literal will also work as shorthand when initializing a set with one or more values as a set collection.

var names: Set<String> = ["David", "Susan", "Robert"]

When initializing the type of set with an array literal that contains values of the same type, it is not necessary to write the type of set. The initialization could have been written in a shorter form:

var names: Set = ["David", "Susan", "Robert"]

Because all values in the array literal are of the same type, Swift infers that Set<String> is the correct type to use for the names variable.

Accessing and Modifying a Set

The count and isEmpty properties work the same way with a set as they do with an array.
Calling the set’s insert method adds a new item to a set.


You can remove an item from a set by calling the set’s remove method. The item is removed if it’s a member of the set, and the removed value is returned. It returns nil if the item is not contained in the set. Alternatively, use the set’s removeAll() method to remove all of the items in a set.

The contains method tells you whether or not a particular item is present in the set.

if names.contains("James") {
 print("James is here.")
} else {
 print("James is not with us.")

Iterating Over a Set

You can iterate over the values in a set with a for-in loop.

for name in names {

Since Swift’s Set type does not provide defined ordering, use the sort() method to iterate over the values of a set in a specific order.

for name in names.sorted() {

Set Operations

Swift allows you to efficiently perform fundamental set operations, such as combining sets, determining which values two sets have in common, or determining whether two sets contain all, some, or none of the same values.

Fundamental Set Operations

The illustration below depicts sets a and b, and shows the results of various set operations, as represented by the shaded regions:

The intersect method creates a new set, with only the values common to both sets.
The exclusiveOr method creates a new set with values in either set, but not both.
The union method creates a new set with all of the values in both sets.
The subtract method creates a new set with values not in the specified set.

For example, to combine the two sets: let oddDigits: Set = [1, 3, 5, 7, 9]

let evenDigits: Set = [0, 2, 4, 6, 8]
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Set Membership and Equality

The illustration below depicts three sets: a, b, and c. The overlapping regions represent the elements that are shared among sets.
Set a is a superset of set b, because a contains all elements in b.
Conversely, set b is a subset of set a, because all elements in b are also contained by a.
Sets b and c are disjointed with one another, because they share no elements in common.
“is equal” operator (==): Determines whether two sets contain all of the same values.
isSubsetOf method: Determines whether all of the values of a set are contained in the specified set.
isSupersetOf method: Determines whether a set contains all of the values in a specified set.
isStrictSubsetOf or isStrictSupersetOf method: Determines whether a set is a subset or superset of, but not equal to, a specified set.
isDisjointWith method: determines whether two sets have any values in common.
Spend a little time playing with the methods to learn and understand their functions.

Sets & Set Operations