Learning Swift – Collections [Sets]

learning swift collections sets

Hello Friends,

Megha here.

In our previous post we studied arrays.

And today we will be learning SETS.

What are sets?

A set is just like an array. Only the difference being that sets have distinct values and no ordering.

example: A set of first four letters of an alphabet. [“a”, “b”, “c”, “d”] 

 

 

How do we initialize a set in Swift? 

We do the initialization using initializer syntax:

Let’s initialize a set containing ages of our friends.

var age = Set<Int>()

var age followed by Set, Int in open brackets, ending with function brackets.

When we print age, we see that it’s empty.

So, we need to add in the ages using 

age = [38, 65, 97]

Now when we print age, we see it has three numbers 38, 65, 97.

The second method of initialization is using array literal.

For this one, we will use an array of colors.

var color : Set<String> = ["Red", "Green", "Yellow"]

The variable color is declared as a set if string values are written using Set<String>. The variable color can only save String values as we specified the type as String. Since we used three colors to initialize the set, color set is not empty.

In arrays, we used [String], but here we use Set<String> as Set type can not be inferred only with String in double brackets, word Set is very important.

This initialization can be done using only “Set” as Swift can infer the type of elements of the set because of its type inference property.

First commenting this initialization, we write

var color : Set = ["Red", "Green", "Yellow"]

Next, we work upon the “Operations on Set”

The first operation is “Inserting an element into the set“. For this, we will insert color “Blue” in the set “color”.

color.insert("Blue")

We can see here in the result bar that “Blue” has been added to set “color”

To confirm, we can print color.

A difference from the array can be seen here, we have not specified the location where we want the blue to be placed. Because unlike arrays, sets are unordered lists. So blue is added at a random position.

The subsequent function is “Removing an element from the set“. For this, we will remove color “Green” from the set.

color.remove("Green")

When we print the set now, the result is devoid of “Green”.

Now we move on to “Contain function: to check whether an element is inside the set”. Again for this first, we will check whether alphabet “a” is inside set and then check whether “Red” is inside the set.

print(color.contains("a"))

We can see the result here in the result are is “false” as “a” is not inside set color.

print(color.contains("Red"))

Red is inside set the color and so the result is true.

Next is the method to count the number of elements in the set.

print(color.count)

Clearly, the answer is 3 as seen here.

Apart from all these operations, there are certain operations that are performed on sets as we have studied in maths.

Let’s see one by one all the operations:

Consider two sets :

A = { “a”, “e”, “i”, “o”, “u” }

B = { “l”, “a”, “u”, “r”, “e”, “n” }

The two circles here denote sets A and B.

 

Since { “a”, “e”, “u”} is common in two sets, we place them in the intersecting of two circles.

{ “a”, “e”, “u”} is also the result of intersection of sets A and B. The yellow color is used to highlight An intersection B

Union operation on the sets leads to a set of all elements from A and B.

So here union of set A and B will have {“a”, “e”, “i”, “o”, “u”,  “l”, “r”, “n”} with { “a”, “e”, “u”} only once. As we have seen earlier sets cannot have repeated values, hence no repeated values here.

On this slide, the yellow color is used to show the union of sets A and B. 

Symmetric Difference gives values in each set but not in both. Here they will be { “o”, “i”, “l”, “r”, “n”}

Subtracting B from A will give values in A which are not in B. ie { “o”, “i” }

Now we will perform all these operations in Swift. 

We have taken two sets:      

var vowels : Set = ["a", "e", "i", "o", "u"] 

var nameLetters : Set = ["l", "a", "u", "r", "e", "n"]

The first operation is Intersection

print(nameLetters.intersection(vowels))

The result is { “a”, “e”, “u”} as expected

Next is Union

print(nameLetters.union(vowels))

The result is all letters in both sets : [“a”, “i”, “r”, “n”, “u”, “e”, “l”, “o”]

For Symmetric Difference

print(nameLetters.symmetricDifference(vowels))

we get [“o”, “l”, “r”, “i”, “n”] as expected

The last is subtraction

print(nameLetters.subtracting(vowels))

The result is  [“l”, “r”, “n”] as expected.

With this we complete Sets. In next post, we will cover Dictionaries, the last of collections.

The code for this post is available here.

 

 

 

 

 

 

 

 

 


One response to “Learning Swift – Collections [Sets]”