Sets In Swift Programming Language

Introduction

In this article, we will learn how to implement the concept of Sets in Swift programming language. Like other languages, Set is a new concept and it's totally related to math. Likewise in math, we see sets and operations like union, intersection, etc. The snapshots in my article are of Xcode Editor. I will run all the lines of code and show its snapshots to you.

What are Sets?

Set is an unordered collection of unique values of the same type. It is extremely useful when you ensure that the repeating items in a set only appear one time and the order of items is not important. 

Creating Sets

The declaration of Set is similar to an array; the only key difference between the array and the set is, when you declare a Set you use a keyword “Set” which means the list of items is treated like a Set.

let setOne: Set<Int>

You can also initialize a set similar to an array, like

let set=Set<Int> ()

Set Literals

Sets don’t have their own literals, so we use array literals to create a set with initial values.

let array= [11,22,33,44]

This is simple array. So, we use array literals to create a set, like this:

let arrayset : Set<Int>=[11,22,33,44]

Accessing Element

Elements in sets don’t have any indices or keys. We have some built-in methods in array like isEmpty(). These types of methods are used in a set, too. I will apply some methods on set.

code

Code

  1. let SetA:Set<Int>=[11,22,33,44]  
  2.   
  3. print(SetA)  
  4. print(SetA.first)  
  5. print(SetA.first!)  
  6. print(SetA.contains(11))  
  7. print(SetA.contains(77))  
Adding and removing element

Adding and removing elements in a set is same as an array. We use insert() method to add the element in set and remove() method to delete an item from set. We can see it through an example and apply add and remove method on it.

code

Code
  1. var SetA:Set<Int>=[11,22,33,44]  
  2.   
  3. print(SetA.insert(88))  
  4. print(SetA)  
  5.   
  6. SetA.remove(88)  
  7. print(SetA)  
Set Operation

One of the most powerful features of set is that it supports mathematical operations, like union, intersection and etc, when you combine two sets.

Union: Creates a new set with all the values from both the sets

Intersect: Creates a new set only with the values that are common in both sets.
Subtract: Creates a new set by removing values which appear in the second set. Here, it depends on which set you want to subtract, like A-B or B-A.
Exclusive-Or: Creates a new set with the values that appear in one of the sets but not both.

code

Code
  1. var SetA:Set<Int>=[11,22,33,44]  
  2. var SetB:Set<Int>=[1,2,3,4]  
  3.   
  4. // Union Set  
  5. print(SetA.union(SetB))  
  6.   
  7. // Intersection  
  8. print(SetA.intersect(SetB))  
  9.   
  10. // Subtract  
  11. print(SetB.subtract(SetA))  
  12. print(SetA.subtract(SetB))  
  13.   
  14. //Exculsive Or  
  15. print(SetA.exclusiveOr(SetB))  
Nesting Sets

Like loops and if-else condition, you can also make nesting sets, like this

code

Code
  1. var SetA:Set<Int>=[11,22,33,44]  
  2. var SetB:Set<Int>=[1,2,3,4]  
  3.   
  4. // Nested Sets  
  5. var combineSets : Set = [SetA,SetB]  
Point of Interest
  • Sets are the unordered collection of unique values.
  • Sets are most useful when you need to find out if the element is included in the collection or not
  • You can initialize sets from array and vice versa

Conclusion

This is all about sets. I covered almost each and every thing. If you face any confusion about this topic, you can comment in the article and ask me.


Similar Articles