What are Sets in Swift
Job-ready Online Courses: Click for Success - Start Now!
Sets are a type of collection data type in Swift. It stores unique elements without any order. A set cannot contain duplicate characters in it. In this article, we’ll discuss how to create and use sets in Swift. We’ll also learn concepts related to sets with the help of relevant examples.
Create a Set in Swift
We can create a set by mentioning the data type Set in the type annotation while declaring a set variable.
var setExample: Set = [12, 61, 23, 78] print(setExample)
Output:
[23, 61, 78, 12]
We can also define the data type of the elements in the set. We can do it by enclosing the data type of the elements within angular brackets <>.
var setExample: Set<Int> = [12, 61, 23, 78] print(setExample)
Output:
[78, 61, 23, 12]
We can also create a set instance and assign values it to later.
var setExample = Set<Int>()
Access and Modify Set elements
We use the following properties and methods to access and modify the set and its elements.
| Property/Method | Description |
| count | Returns the total number of unique elements in the set |
| isEmpty | Check if the given set is empty or not. |
| insert(element) | Adds a new element to the set |
| contains(element) | Check if the set contains certain elements or not. |
| remove(element) | Removes the element from the set |
| removeFirst(element) | Removes the element at first from the set |
| removeAll(element) | Removes all the elements from the set |
var setExample: Set = ["DataFlair", "Swift", "Sets", "Collections", "Swift"]
print(setExample)
// checks set is empty or not
print("Set Empty?:", setExample.isEmpty)
// insets new element to the set
setExample.insert("Programming")
print(setExample)
// returns the number of elements in the set that are unique
print("Set count:", setExample.count)
// removes element from the set
setExample.remove("Swift")
print(setExample)
// checks if the set contains the particular element
print("Set contains DataFlair?:", setExample.contains("DataFlair"))Output:
[“Collections”, “Swift”, “Sets”, “DataFlair”]
Set Empty?: false
[“Collections”, “Swift”, “Sets”, “Programming”, “DataFlair”]
Set count: 5
[“Collections”, “Sets”, “Programming”, “DataFlair”]
Set contains DataFlair?: true
Methods for Sets in Swift
Swift offers other inbuilt methods for sets. The table below depicts the same.
| Property/Method | Description |
| sorted() | Sorts the elements in the set |
| forEach() | Performs specific actions for every element in the set |
| randomElement() | Returns a random element from the set |
| firstIndex() | Return the index of the element from the set. |
Loops and Sets
We use a for-in loop to iterate through the elements in a set.
var setExample: Set = ["DataFlair", "Swift", "Sets", "Collections"]
for element in setExample{
print(element)
}Output:
Swift
Sets
Collections
DataFlair
Set Operations
Swift provides inbuilt functions to perform set operations. Operations include the union of sets, the intersection of sets, a difference of sets and symmetric difference of sets. It is similar to mathematical set operations. Following explains these operations in detail.
Union of sets
The operation union of sets includes all the elements from both sets. Since the final result is a set, no duplicate elements are present.
var set1: Set = [23,25,27,29] var set2: Set = [21, 25, 27] var set3: Set = set1.union(set2) print(set3)
Output:
[23, 25, 27, 21, 29]
Intersection of sets
The operation intersection of sets includes only the common elements from both sets. Since the final result is a set, no duplicate elements are present.
var set1: Set = [23,25,27,29] var set2: Set = [21, 25, 27] var set3: Set = set1.intersection(set2) print(set3)
Output:
[27, 25]
Difference of sets
The operation difference of sets results in a final set where all the elements present in the first set are included, but the ones also present in the second set are excluded.
var set1: Set = [23,25,27,29] var set2: Set = [21, 25, 27] var set3: Set = set1.subtracting(set2) var set4: Set = set2.subtracting(set1) print(set3) print(set4)
Output:
[29, 23]
[21]
Symmetric Difference of sets
The operation of the symmetric difference of sets results in a final set which includes all the elements of the first and second sets but excludes the elements common in both sets.
var set1: Set = [23,25,27,29] var set2: Set = [21, 25, 27] var set3: Set = set1.symmetricDifference(set2) print(set3)
Output:
[29, 21, 23]
Set Membership in Swift
Swift offers more methods for Swift to verify and test the membership of elements of a set.
The following table shows these methods and explains what they do.
| Method | Description |
| contains(element) | Checks whether an element is present in a set or not |
| == | Check if the two sets are the same or not |
| isSubset(of:) | Check if a set consists of all elements of the given set in the parameter. |
| isSuperset(of:) | Checks if the elements of the set are present in the given set or not |
| isStrictSubset(of:) | Check if the given set is a subset but not equal to the set. |
| isStrictSuperset(of:) | Checks if the given set is a superset but not equal to the set. |
| isDisjoint(with:) | Check if all the elements are different in the two sets |
var set1: Set = [41, 42, 43, 44, 45, 46, 47, 48]
var set2: Set = [42, 44, 45, 46, 48]
print("Contains 44 in set 1?", set1.contains(42))
print("Contains 44 in set 2?", set2.contains(42))
print("Are the sets equal?", set1 == set2)
print("set1 subset of set2?", set1.isSubset(of: set2))
print("set2 subset of set1?", set2.isSubset(of: set1))
print("set1 superset of set2?", set1.isSuperset(of: set2))
print("set2 superset of set1?", set2.isSuperset(of: set1))
print("set1 strict subset of set2?", set1.isStrictSubset(of: set2))
print("set2 strict subset of set1?", set2.isStrictSubset(of: set1))
print("set1 strict superset of set2?", set1.isStrictSuperset(of: set2))
print("set2 strict superset of set1?", set2.isStrictSuperset(of: set1))
print("are the sets disjoint?", set1.isDisjoint(with: set2))Output:
Contain 44 in set 1? true
Contain 44 in set 2? true
Are the sets equal? false
set1 subset of set2? false
set2 subset of set1? true
set1 superset of set2? true
set2 superset of set1? false
set1 strict subset of set2? false
set2 strict subset of set1? true
set1 strict superset of set2? true
set2 strict superset of set1? false
Are the sets disjoint? false
Conclusion
Swift provides a very unique collection type data type known as Set. It stores all unique elements of the same data type. Swift also offers several properties and methods to manipulate the elements in the sets based on our use. In this article, we have covered different ways to use Swift sets and their methods and properties to our convenience.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google





