Swift Collections : Sets

Let’s talk about one of the other collections in swift.The sets.Unlike arrays, sets are unordered collections. The other difference is that the set’s items must be unique. So there won’t be any duplication in sets. We can define an empty set or we can initialize it with default items. The following code shows how to define an empty set :

var mySet = Set<String>()
var yourSet = Set<Int>()
let hisSet = Set<Double>()

We have defined mutable sets above in the first two lines (mySet, yourSet) while the last line is an immutable set. That means once hisSet got the value, we won’t be able to change it again. We can also initiate a set with default values. The following example shows how to do it :

var ages = Set([25, 30, 32])
 
//Creates a immutable set of the String type.  
let daysInWeek = Set(["mon", "tue", "wed", "thu", "fri", "sat","sun"])

Please pay attention in the above example. Even if we define the daysInWeek set with ordered items, we can’t read them in order just as we defined them. So what about inserting an item ? We use the insert() method to insert items into a set. If the item that we are trying to insert is already exist in the set then our attempt will be ignored. Following example shows how to insert an item into a set :

var names = Set<String>()
names.insert("Isaac")
names.insert("Adam")
names.insert("David")

The insert() method also returns a tuple that allows us to verify if the item was inserted successfully. Let’s look at the example below to understand it :

var names = Set<String>()
names.insert("Isaac")
names.insert("Adam")
names.insert("David")
 
var results = names.insert("Adam")
if results.inserted {
  print("Item was inserted successfully")
} else {
  print("Failed during inserting")
}

When we run the code above  it will print out “Failed during inserting” . Because there is already an item called “Adam” in the names set.

We can use count property to get the count of a set. To get the items count of the names set that we used in the above example we can use names.count  . We can also verify whether a set contains a certain item. To do that we can use the contains() method. If the item exists in the set, the return value will be true otherwise it will return false. Look at the code below :

var names = Set<String>()
names.insert("Isaac")
names.insert("Adam")
names.insert("David")
 
var itemVerify = names.contain("David")
if itemVerify {
  print("Item exists")
} else {
  print("Item doesn't exist")
}

The above code will print out “Item exists” when we run it. What about removing items from a set ? To do that we can use remove() method. This method also returns the removed item. There is also a method called removeAll() to remove all the items in a set. Below example shows how to use the remove method.

var names = Set<String>()
names.insert("Isaac")
names.insert("Adam")
names.insert("David")
 
var removedName = names.remove("David")
print("\(removedName) has been removed")

To iterate through a set we can use the for-in loop. This will allow us to get each item of a set. Let’s look at the example below :

var names = Set<String>()
names.insert("Isaac")
names.insert("Adam")
names.insert("David")
 
for name in names {
    print(name)
}

I know in the last few examples I used the same names  set. It’s because I didn’t want to define a new set in each example. I think you got the main purpose of each method.

Alright. Let’s finish here. We’ve learned some basics about sets in this post. See you in the next one…

You may also like...