Array VS Set in Swift
By: Ahmed Menaim
Tags: iOS, Arrays, Set, Xcode, Swift

After finishing this article you will be able to know the differences between array and set in swift language.

Let’s use a simple example to be able to understand it in an easy way:

Suppose we are in a small school and you need to initialize each class with its students but here we have only three classes as we start as a small school:

var classA = ["Max", "Niro", "Geace","Clary","Maghnos","Raul","Sam"]



var classB : [String] = []

classB = ["Asta","Neur","Magnam","Rose","Santiago","Alex"]



var classC: Array<String> = Array()

classC = ["Niro", "Rose","Sam","Muller","Nymar", "Joe","Bella"]



let classD = ["Scoot","Lydia","Alex","Sam","Clary","Bella","Alison","Sepastian"]

In the above block of code I have initialized every class with its student names and I’ve used more than way to declare the array, the first & the last one are the most used which you don’t need to mention data types or anything which called in swift Type inference.

The one which used in classB is called short naming which I here initialized the data type of the array and set it as an empty array and the last one which is used in classC is called long naming and here I’ve created an empty array using keyword array and initialized its type.

Now we have our classes but it seems that the teacher doesn’t know where the student who called Max so he figured out that he needs to check in each class array

if classA.contains("Max") {

print("Max is here")

}

Output: Max is here

It seems that the teacher was lucky as he found Max in the first class so he doesn’t need to check the rest of the classes but he thought that it will be better to check all Max’s friends in the same class so he will print all the array and you can do this by two ways:

for student in classA {

print(student)

}

output: Max

Niro

Geace

Clary

Maghnos

Raul

Sam

or you can print it so easy as an array:

print(classA)

Output: [“Max”, “Niro”, “Geace”, “Clary”, “Maghnos”, “Raul”, “Sam”]

Now the teacher decides to get the first name in each class so he will do the below:

print(classA.first!) // Max

print(classB.first!) // Asta

print(classC.first!) // Niro

print(classD.first!) //Scoot

So in the previous block of code the teacher just got the first student in each class using .first if you don’t know why did we put ! mark don’t worry it’s already explained here.

After two days passed in the semester there are two new students came and they will be distributed to the classes, their names are Danny & Samantha so one of them will go to classB and the second one to classC so the teacher now needs to add them to the array:

classB.append("Danny")

classC.append("Samantha")

print(classB) // ["Asta", "Neur", "Magnam", "Rose", "Santiago", "Alex", "Danny"]

print(classC) // ["Niro", "Rose", "Sam", "Muller", "Nymar", "Joe", "Bella", "Samantha"]

and then they discovered that there are twin students need to be with each other in the same class, they called Aiden & Ethan so they will be in classA:

classA.append(contentsOf: ["Aiden" , "Ethan"])

print(classA)

Output: [“Max”, “Niro”, “Geace”, “Clary”, “Maghnos”, “Raul”, “Sam”, “Aiden”, “Ethan”]

The teacher was giving a lesson and discovered that Max isn’t the real name and the real name is “Maximum” so he has to change the name now:

if let i = classA.firstIndex(of: "Max") {

classA[i] = "Maximum"

}

print(classA)

Output: [“Maximum”, “Niro”, “Geace”, “Clary”, “Maghnos”, “Raul”, “Sam”, “Aiden”, “Ethan”]

firstIndex means the first occurrence so the first time you found “Max” please change it with “Maximum” but if there is another student called “Max” it won’t be changed.

unfortunately the twin brothers went to other state so we need to remove them now:

classA.removeSubrange(7..<9)

print(classA)

Output: [“Maximum”, “Niro”, “Geace”, “Clary”, “Maghnos”, “Raul”, “Sam”]

as you know array is 0 index and when we add “Aiden” & “Ethan”, they have been added at the end of our array which means that they are at index 7 & 8 so when I typed 7..<9 means that I need to remove only 8th & 9th element in my array.

Another way to do it

print(classA.count) // 9

classA.remove(at: 8) print(classA.count) // 8

classA.remove(at: 7) print(classA) // ["Maximum", "Niro", "Geace", "Clary", "Maghnos", "Raul", "Sam"]

You have 9 students in your classA and you removed the last student so now you have 8 students in your classA, You still need to remove the last one as the twin brothers were at last two indices in your array.

But all of this was to much to do, if you already know that they are at the last two indices so you can do this:

classA.removeLast(2)

print(classA)

Output: [“Maximum”, “Niro”, “Geace”, “Clary”, “Maghnos”, “Raul”, “Sam”]

After finishing the first semester in the school, the manager decided to make only one class from classB and classC and he will call it classBC:

var classBC = classB + classC

print(classBC)

Output: [“Asta”, “Neur”, “Magnam”, “Rose”, “Santiago”, “Alex”, “Danny”, “Niro”, “Rose”, “Sam”, “Muller”, “Nymar”, “Joe”, “Bella”, “Samantha”]

The manager is happy now but he is still has an issue, he dicovered that he has two or more students has the same first name so he decided to let only one with the name as he doesn’t need repeated names anymore.

Here the best way to do it is to create a set of students instead of array:

var classBCSet = Set(classBC)

print(classBCSet)

Output: [“Santiago”, “Nymar”, “Alex”, “Bella”, “Muller”, “Neur”, “Magnam”, “Joe”, “Niro”, “Rose”, “Asta”, “Danny”, “Sam”, “Samantha”]

Now you have a clear set without any repeated names, Let’s talk a little about set

One of the most important things in set is that set never has a repeated values which makes it faster than array , also sets has no sequence or indices that’s why at each time you run the code you will have a different output (different only in arrangement).

The manager likes sets more than arrays and that’s why he will manage the school now using sets especially sets will help him with some functions.

The manager now needs to know how many students their names are the same in different classes:

var classASet = Set(classA)

var classDSet = Set(classD)



print(classASet.intersection(classDSet)) // ["Sam", "Clary"]

print(classASet.intersection(classBCSet)) // ["Niro", "Sam"]

print(classBCSet.intersection(classDSet)) // ["Alex", "Sam", "Bella"]

In the previous code I just made a set for the rest of my classes and then get the intersection between them “The common students’ names”

The manager now needs to get the one who named “Clary” or “Raul” in classA:

print (classA.filter{$0 == "Clary" || $0 == "Raul"})

Output: [“Clary”, “Raul”]

Here we made a filter to get the names we want.

Once the second semester starts the manager brings two new students “Isabel” & “Lisa” and he decided to distribute them to classASet & classDSet:

classASet.insert("Lisa")

classDSet.insert("Isabel")

print(classASet)

// ["Maximum", "Maghnos", "Sam", "Raul", "Geace", "Niro", "Clary", "Lisa"]

print(classDSet)

//["Lydia", "Sam", "Clary", "Bella", "Alison", "Alex", "Isabel", "Sepastian", "Scoot"]

unfortunately “Bella” decided to leave the school:

classBCSet.remove("Bella")

print(classBCSet)

Output: [“Neur”, “Sam”, “Danny”, “Niro”, “Rose”, “Alex”, “Nymar”, “Samantha”, “Joe”, “Magnam”, “Santiago”, “Asta”, “Muller”]

After finishing the year the manager decided to make a party in one class so he decided to concatenate all classes in one class:

var partyClassSet = classBCSet.union(classASet).union(classDSet)

print(partyClassSet)

Output: [“Lisa”, “Magnam”, “Sepastian”, “Sam”, “Nymar”, “Maghnos”, “Santiago”, “Joe”, “Samantha”, “Muller”, “Geace”, “Raul”, “Neur”, “Danny”, “Scoot”, “Alex”, “Lydia”, “Rose”, “Bella”, “Isabel”, “Alison”, “Clary”, “Maximum”, “Niro”, “Asta”]

Finally the year has been finished and manager is so happy with his school system and small classes.