Swift: Sets

Sets in Swift:

Let’s begin with Sets in Swift.

Swift Sets:

A set is a data type that can use to contain unique elements of the same data type.

var easternCountries: Set = ["Russia", "Australia", "Fiji"]

Empty Sets:

An empty set is one that doesn’t have any values in it.

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

Populated Sets:

Use the Set keyword before the assignment operator to create a set with populated values.

The set’s values must be surrounded in brackets [] and separated by commas ,.

var animals: Set = ["cat", "dog", "horse", "lion", "tiger", "cow"]

.insert():

Append to a set to insert a single value. Insert the new value inside the parentheses () after calling .insert() on a set.

var colours: Set = ["Blue", "Purple", "Yellow"]
 
// Add a new element
colours.insert("Pink")

.remove() and .removeAll() Methods:

Append .remove() to a set with the value to be removed inside the parentheses () to remove a single value from a set.

To remove every single value from a set at once, append .removeAll() to a set.

var evenNumbers: Set = [2, 3, 4, 6, 8]
 
// Remove an existing element
evenNumbers.remove(3)
 
// Remove all elements
evenNumbers.removeAll()

.contains():

When you append .contains() to an existing set an item in the (). So, you’ll get a true or false value showing if the item exists within the set.

var fruits: Set = ["apple", "mango", "banana"]
 
print(fruits.contains("kiwi")) // Prints: false
 
if fruits.contains("mango"){
  print("There is a mango!")
} else {
  print("Where is a mango?")
}
// Prints: There is a mango!

Iterating Over a Set:

To iterate over each item in a set, use a forin loop.

var recipe: Set = ["ketchup", "sausage", "mustard", "bun"]
 
for ingredient in recipe {
  print ("add \(ingredient) in the recipe.")
}

.isEmpty Property:

Use the built-in property to your benefit. The .isEmpty is used to see if a set contains no values.

var nullSet = Set<String>()
 
print(nullSet.isEmpty)  // Prints: true
 
var numSet: Set = [1, 2, 3, 4, 5]
 
print(numSet.isEmpty) // Prints: false

.count Property:

The .count property returns the total number of elements in a set.

var employees: Set = ["Lisa", "Alex", "Munro", "Katie", "Lily"]
 
print("There are \(employees.count) employees in the web team.")
// Prints: There are 5 employees in the web team.

.intersection() Operation:

The .intersection() operation creates a new set of elements from the elements of two sets that overlap.

var setOne: Set = [1, 2, 3, 4, 5]
var setTwo: Set = [4, 5, 6, 7, 8]
 
var setThree = setOne.intersection(setTwo)
print(setThree)  // Prints: [5, 4]

.union() Operation:

By combining all of the values from two sets, the .union() action creates a new set.

var setOne: Set = [1, 2, 3, 4, 5]
var setTwo: Set = [4, 5, 6, 7, 8]
 
var setThree = setOne.union(setTwo)
print(setThree)
// Prints: [4, 2, 8, 5, 6, 1, 7, 3]

.symmetricDifference() Operation:

The .symmetricDifference() operation returns a new set containing all non-overlapping values from two sets.

var setOne: Set = [1, 2, 3, 4, 5]
var setTwo: Set = [4, 5, 6, 7, 8]
 
var setThree = setOne.symmetricDifference(setTwo)
print(setThree) 
// Prints: [2, 6, 1, 3, 7, 8]

.subtracting() Operation:

The .subtracting() operation subtracts the values of one second set from another set. So, it stores the remainder in a new set.

var setOne: Set = [1, 2, 3, 4, 5]
var setTwo: Set = [4, 5]
 
var setThree = setOne.subtracting(setTwo)
print(setThree) 
// Prints: [3, 1, 2]

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: