What is Swift Sets?
Swift set is a simple container which can hold multiple values of a data type in an unordered list. It can't store same value twice. Here, the unordered list means you won't get the items in the same manner in which you have enter in the set.
Swift Sets vs. Swift Arrays
The main difference between the Swift set and Swift array is that the Swift set can't store the same value twice unlike the Swift array. Swift sets ensure you that an item appears only once and order of items is not important.
Values stored in a Swift set must be hashable. Hashable means, it must provide a hashValue property. HashValue is used to access the elements of the sets because sets are unordered.
All Swift basic data types (such as String, Int, Double, and Bool) are hashable by default, and can be used as set value types. We can also create our Hashable Type in Swift that can be stored in a set.
How to declare a set in Swift?
We can declare a set that can store some values.
[2, 4, 9, 5, 6, 7, 3, 1, 8]
Declare a set containing duplicate values
[2, 4, 5, 3, 1, 8]
In the above program, we have entered the repeated value of 5 and 8. But in output, we can see that the repeated values have been removed.
Access set elements in Swift
We cannot access elements of set in the same way we access elements of array in Swift because set elements are unordered.
So, we have to access set elements using its methods and properties or using for-in loops.
Access all set elements in Swift
5 6 2 3 1 4
Using remove() method to access selective set elements
We can access the selective elements from the set by using remove() method.
Optional(5) [6, 2, 3, 1, 4]
In the above program, you can see that the remove() method returns an optional string. It is recommended to do optional handling as follows.
Optional handling for using remove() method
5 [6, 2, 3, 1, 4]
Add new elements in a set in Swift
The insert() method is used to add new elements in set in Swift.
To add single element
[2, 4, 5, 6, 7, 3, 1]
In the above example, we have added a new element 7 in a predefined set.
To add multiple elements
[10, 2, 4, 9, 5, 6, 7, 3, 1, 8]
Set operations in Swift
Another advantage of using set is that it facilitates you to perform set operations, i.e. union, intersection etc. It is same as set operation in Mathematics.
Union Set Operation
The union of two sets a and b is the set of all elements combined in set a and set b. It is accessed by using a.union(b).
[8, 2, 9, 4, 5, 7, 6, 3, 1, 0]
Intersection Set Operation
The intersection of two sets a and b is the set of elements which are common in both sets. It is accessed by using a.intercection(b).
Subtracting Set Operation
The subtraction of two sets a and b is the set which contains all elements of a but excludes the elements that also belongs to b. It is accessed by using a.subtracting(b).
[5, 9, 1]
Symmetric Difference Set Operation
The symmetric difference of two sets a and b is the set which contains all elements which are in either of the sets but not in both of them. It is accessed by using a.symmetricDifference(b).
[5, 6, 9, 8]
Some Built in functions and properties