Sets in Swift


Set in Swift is an unordered collection of unique data of the same types. Sets are used to store unique values of the same types. It can not store any duplicate elements.

Suppose, we have to store some employee IDs in a set. Because employee IDs are unique, we can store them in a set.

Below is a diagram of a set where we have stored the IDs of employees we have just discussed:

Set in Swift Diagram
Elements of a set

Creating a set in Swift

Now you are going to learn how to create a set in Swift programming with syntax and examples.

The set keyword is used to create a set in Swift.

Creating an empty Set

Below is the syntax for creating an empty set:

var setName = Set<TYPE>()

In the above syntax, the setName is the variable name of the set, TYPE is the data type of set elements.

Now see the example below where we are creating an empty set:

var firstName = Set<String>()

In this example, we have created a string-type empty set. In this set, we can store string-type items.

If we print the empty set using print(firstName), it will give the output:

[]

You can see there are no elements in the set and showing empty. This is because we have created an empty set and have not inserted any value to it.

Creating a set with some elements

Below is how we are creating a set with some values:

var productID: Set = [167, 343, 22, 192]
print(productID)

Output:

[22, 192, 167, 343]

Note that, we can store the same type of values in the set. In the above example, all the values in the set items are of integer type. So the set is a set of integer type.

Adding and removing elements from set

We can add new elements and remove existing elements from a set.

Adding a new item

We can easily add a new item to a set using the set insert() method in Swift. Below is the example where we are adding an element to a set:

var productID: Set = [1, 2, 3, 4]
// Before adding an element
print(productID)
productID.insert(999)
//After adding an element
print(productID)

Output:

[4, 1, 3, 2]
[4, 1, 3, 999, 2]

In the output above, you can see that we have printed both the set before adding an element and after using the insert() method for adding an element.

Removing an existing set item

In Swift, we can use the remove() method to remove an item from a set. Below is the example:

var productID: Set = [1, 2, 3, 4]
// Before removing an element
print(productID)
productID.remove(4)
//After removing an element
print(productID)

Output:

[3, 2, 4, 1]
[3, 2, 1]

Remove all elements from a set in Swift

Using the removeAll() method, we can remove all the elements from a set in Swift. Below is an example to show how we are doing it:

var productID: Set = [1, 2, 3, 4]
// Before removing all element
print(productID)
productID.removeAll()
//After removing all element
print(productID)

Output:

[2, 3, 1, 4]
[]

Accessing Set elements

Well, it is not possible to access set elements as we do easily in the case of an array. This is because of the fact that the elements of a set are unordered.

But you can access the elements of a set in different ways. You can do it using for-in loop, methods, properties. You are going to learn how to iterate over a set of elements in Swift in the section “Iterate through set elements” in this article soon.

Find number of elements in a set

Using the count property, we can easily find the number of elements in a Swift. We can do it just using one line of code.

Below is the example:

var fruits: Set = ["Apple","Strawberry","Peach","Cherry"]
print(fruits.count)

Output:

4

So we have successfully been able to print the number of elements in a set.

Iterate through set elements

We can iterate through set items just like we do with an array using the for-in loop. Below is the example where we are getting each item from the set and printing it:

var fruits: Set = ["Apple","Strawberry","Peach","Cherry"]

for fruit in fruits {
    print("This is "+fruit)
}

Output:

This is Peach
This is Apple
This is Strawberry
This is Cherry

We can do the same using forEach loop:

var fruits: Set = ["Apple","Strawberry","Peach","Cherry"]

fruits.forEach() {
    print("This is "+$0)
}

In the above example, the output will be the same as you see in the previous one. You can notice that we have not used any argument to represent each item of the set. Swift forEach loop automatically assigns the argument like $0, $1, $3….. $n depending on the number of items.

If you want, you can set the explicit argument in the forEach loop to get set elements like you can see in the example below:

var fruits: Set = ["Apple","Strawberry","Peach","Cherry"]

fruits.forEach() { fruit in
    print("This is "+fruit)
}

In the above example, we are using our own argument instead of using $0.

Proving that a set can’t store duplicate items

Let’s try to create a set with duplicate elements in it:

var fruits: Set = ["Apple","Strawberry","Cherry", "Cherry"]
print(fruits)

In the above example, we have created a set. In this set, we have assigned the “Cherry” twice. But when we print it and run the code, we can see the “Cherry” appears only once and a duplicate item has been removed in the output.

No matter how many same items we assign, it will print only once.

So we can see that in Swift, sets will always have unique elements.


Learn more about set operations: