Published Aug 4, 2021Updated Oct 23, 2022
Contribute to Docs

In Swift, a set is used to store unique elements of the same data type.


var setName = Set<Type>()

var setName: Set = [value1, value2, ...]

Type refers to the data type of the values to be stored in the set.

To create a set populated with values, use the Set keyword before the assignment operator. The values of the set must be contained within brackets [] and separated with commas ,.

Empty Sets

An empty set is a set that contains no values inside.

var team = Set<String>()
// Output: []

Iterating Over a Set

A for-in loop can be used to iterate over each item in a set.

var recipe: Set = ["Chocolate chips", "Eggs", "Flour", "Sugar"]
for ingredient in recipe {
print("Include \(ingredient) in the recipe.")

.isEmpty Property

Use the built-in property .isEmpty to check if a set has no values contained in it.

var emptySet = Set<String>()
print(emptySet.isEmpty) // Prints: true
var populatedSet: Set = [1, 2, 3]
print(populatedSet.isEmpty) // Prints: false

.count Property

The property .count returns the number of elements contained within a set.

var band: Set = ["Guitar", "Bass", "Drums", "Vocals"]
print("There are \(band.count) players in the band.")
// Output: There are 4 players in the band.


There are many set manipulation methods available in the Swift Standard Library, including generic Collection-based methods. Some of these include testing the contents of sets (e.g. .contains() and .isEmpty) while others can modify or manipulate the set entirely (e.g., .map(), .reduce(), and .sorted()). There is also a list of methods applicable to a pair of sets (e.g., .intersection() or .subtracting()).

Below are some methods available for sets:


Checks whether an item exists within the set.
Adds an element at a specified index.
Returns a new set of elements with the overlapping elements of two sets.
Checks whether all elements of a set are present in another set.
Returns a boolean telling whether every element of a given set exists in another set.
Removes and returns a specified element from a set.
Removes every item from a Set in Swift.
Returns a new set containing the elements from the target set that are not in the given set.
Returns a new set with all the elements from two sets that do not overlap.
Returns a new set containing all elements of one set combined with the elements of a given set.

All contributors

Looking to contribute?

Learn Swift on Codecademy