Trusted answers to developer questions

How to use set in Swift

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

A set is a collection of unordered unique values. Therefore, it cannot contain a duplicate element.

Let’s create a set:

let groceryList: Set = ["banana", "sugar", "salt", "sugar", "snacks"]

We created a set of Strings above using the Set keyword. A set can also be created by specifying the element type of the set like:

let numbers: Set<Integer> = [1,2,3,4,5]

Example

import Swift
let groceryList: Set<String> = [ "banana", "banana", "sugar", "salt", "sugar", "snacks"]
print("groceryList : \(groceryList)")

In the code above, we created a set (groceryList) with values. In the values, the element “banana” occurs two times. But when we print the groceryList, the duplicate value will not be present because the set cannot contain duplicate elements. Additionally, the order of the elements can be changed as the set does not maintain its elements in a particular order.

To create an empty set, we can use:

let emptySet = Set<Int>()

// or 

emptySet = []

Important Properties and Methods

  • count returns the number of elements of the set.

  • isEmpty returns true when the set is empty.

  • first returns the first element of the set.

  • insert(element) adds an element to the set if it is not present.

  • removeFirst() removes the first element of the set.

  • remove(element) removes the specified element of the set.

  • contains(element) returns true if the element is present in the set.

  • randomElement() returns a random element from the set.

  • first(where:) returns items that satisfy a given predicate.

Find more properties and functions in the Apple Developer page.

var numbers = Set<Int>()
print("Is set empty => \(numbers.isEmpty) ")
print("Adding 1")
numbers.insert(1)
print("Is set empty => \(numbers.isEmpty) ")
print("The set is \(numbers)")
print("Adding duplicates")
numbers.insert(1)
print("After adding duplicate value 1. The set is \(numbers)")
numbers.insert(3)
numbers.insert(2)
numbers.insert(4)
numbers.insert(5)
numbers.insert(6)
numbers.insert(7)
if let firstNumber = numbers.first {
print("The first number is ", firstNumber)
}
var count = numbers.count
print("No.of.elements in Set is ", count)
if let rand = numbers.randomElement() {
print("Getting Random number",rand)
}
if let rand = numbers.randomElement() {
print("Getting Random number",rand)
}
numbers.remove(1)
print("Checking if numbers contain 1", numbers.contains(1))

In the code above, we implemented basic operations such as “insert,” “remove,” checking if a value is present in the set, getting the first element, and generating a random element. In a set, we can obtain a union, intersection, subtraction, and symmetric-difference of two sets.

RELATED TAGS

sets
duplicate
element
Did you find this helpful?