Updated April 7, 2023
Introduction to Swift sets
Swift Sets is one of the most desirous and powerful data structures in the swift programming language. Sets in Swift are the containers that hold various types of elements in the form of an unordered list, with the exception that the elements present in the list must be unique and should appear only once in the entire list. The list must possess elements in a hashable manner which means that it should provide hash type values to the elements within the list. The major advantage of the hashable property is the easy accessibility of elements in the unordered list.
How to declare sets in swift?
Declaration of sets in swift has a syntax which is as follows :
let Int_dec_Set:Set = [0,5,2,6,1] // [ ] this is used to create empty Set and [any set of values].
print(Int_dec_Set)
Here the Int_dec_Set is the name of the set which will hold the data type prefixed with a value called set, which signifies that the set is to be used for manipulation.
How to add elements in sets?
The addition of elements in sets plays a pivotal role in the insertion of elements according to the requirement and wherever required.
Any new element in sets is added using the insert() method in the swift programming language. As sets in swift consists of the unordered list, the element can be added anywhere within the list. No specific order is maintained.
The examples section will clarify the process of adding elements in sets.
How to delete elements in sets?
Deleting elements in sets is performed using the remove () method, which is used to remove the specified element from the unordered list.
If the scenario is to delete any element from the list, remove() method, which will either return the member element if it was part of the list or give nil value. Further examples section will provide more clarity to the deletion section of elements.
Various operations performed on sets in swift.
The beauty of using sets and the flexibility it provides to the programmers are the operations that set in swift programming language provides. Thus, there are various operations on sets that are performed in swift and are as follows :
- Union: This operation is performed over the sets of elements x and y, which are present in x or y or both x and y of the sets.
- Intersection: This operation is performed over the set of elements x and y, consisting of all the elements present in x and those present in y.
- Subtracting: This operation is performed on two sets, x and y, where the set will consist of all the elements present in x and will remove all those elements which are present in y.
- Symmetric Difference: This operation is performed over two sets, x and y, where the elements will be present in any one of the two mentioned sets.
- Set Membership and Equality Operations
Set Equality: Set Equality operation is performed to check elements in the sets whether they contain the same element or different elements.
Set Membership: Set membership is a kind of method which is used for determining any relation between two sets or subsets, and the operation includes the method like is a subset(of:), is a superset(of:), isDisjoint(with:), is strictSuperset(of:)
Examples
Here are the following examples mention below
Example #1
This program demonstrates the addition of elements using the insert() method where the birds are added as part of the existing unordered list as shown in the output.
import Foundation
import Glibc
var sm_add_set:Set = ["fruits", "cars", "animals", "bikes"]
sm_add_set.insert("birds")
print(sm_add_set)
Output:
Example #2
This program demonstrates the optional removal of the specified element from the unordered list where the specified elements can be removed or deleted using values in the set as shown in the output. Here is the optional removal, which is the more recommended method.
import Foundation
import Glibc
var s_del_int_Set:Set = [4,3,2,5,8]
if let sm_ltl_Val = s_del_int_Set.remove(5)
{
print(sm_ltl_Val)
print(s_del_int_Set)
}
else
{
print("no_element_is_found_for_deletion")
}
Output:
Example #3
This program demonstrates two sets x_1 and y_1, where the union is applied for operations, and the output is shown as below.
import Foundation
import Glibc
let x_1: Set = ["truck", "car", "jeep", "scotty", "bike"]
let y_2: Set = ["truck", "suv", "scooty", "sedans", "bicycle"]
print(x_1.union(y_2))
Output:
Example #4
This program demonstrates two sets x_1 and y_1, where the intersection is applied for operations and manipulation where the output is shown as below.
import Foundation
import Glibc
let x_1: Set = ["truck", "car", "jeep", "scotty", "bike"]
let y_2: Set = ["truck", "suv", "scooty", "sedans", "bicycle"]
print(x_1.intersection(y_2))
Output:
Example #5
This program demonstrates the sets e_0 and f_1 where the operation is performed to subtract the elements from one set to another set as shown in the output.
import Foundation
import Glibc
let e_0: Set = [10, 12, 16, 5, 10]
let f_1: Set = [1, 2, 6, 9, 8]
print(e_0.subtracting(f_1))
Output:
Example #6
This program demonstrates the sets p_0 and q_1 where the operation is performed to get the symmetric difference as shown in the output.
import Foundation
import Glibc
let p_0: Set = [25,12,10,14,16]
let q_1: Set = [12,10,15,18,19]
print(p_0.symmetricDifference(q_1))
Output:
Example #7
This program demonstrates the set m_0, n_0, and o_0, which is used for representing the set equality operation as shown in the output.
import Foundation
import Glibc
let m_0: Set = [12, 2, 8, 10, 13]
let n_0: Set = [0, 1, 9, 17, 12]
let o_0:Set = [9, 7, 3, 1, 5]
if m_0 == n_0 {
print("m_0 and n_0 both appears same.")
} else {
print("m_0 and n_0 are different.")
}
if m_0 == o_0 {
print("m_0 and o_0 both appears same.")
} else {
print("m_0 and o_0 are different.")
}
Output:
Conclusion
The Swift set plays a very significant role in the Swift programming language as it gives programmers the ability to access the elements and manipulate them according to the requirement. Sets operations and methods give an added advantage in organising and playing around with the data types present in an unordered set.
Recommended Articles
We hope that this EDUCBA information on “Swift sets” was beneficial to you. You can view EDUCBA’s recommended articles for more information.