Adding elements to a swift sets
For adding elements to a swift set, the insert() method is used.
var StudentID: Set = [24, 36, 41, 55, 60]
print(“Initial Set: \(studentID)”)
// Inserting element
numbers.insert(32)
print(“Updated Set: \ (numbers)”)
Output:
Initial Set: [24, 36, 41, 55, 60]
Updated Set: [ 24, 36, 41, 55, 60, 32]
Removing Element from the Swift sets
For removing a specified element from the set, the remove() method is used.
var languages: Set = ["Swift", "CPP", "Python"]
print("Initial Set: \(languages)")
// Remove CPP from a set
let removedValue = languages.remove("Java")
print("Set after remove(): \(languages)")
Output:
Initial Set: ["Python", "CPP", "Swift"]
Set after remove(): ["Python", "Swift"]
Swift set operation
Swift Set provides various built-in methods for mathematical set operations like union, subtraction, intersection, and symmetric difference.
To perform the set union operation, the union() method is used.
// First set
let setA: Set = [1, 3, 5]
print("Set A: ", setA)
// Second set
let setB: Set = [0, 2, 4]
print("Set B: ", setB)
// Perform union operation
print("Union: ", setA.union(setB))
Output:
Set A: [1, 5, 3]
Set B: [0, 2, 4]
Union: [0, 5, 2, 4, 1, 3]
- The intersection between two sets
To perform an intersection between two sets, the intersection() method is used.
// First set
let setA: Set = [1, 3, 5]
print("Set A: ", setA)
// Second set
let setB: Set = [1, 2, 3]
print("Set B: ", setB)
// Perform intersection operation
print("Intersection: ", setA.intersection(setB))
Output:
Set A: [5, 3, 1]
Set B: [1, 2, 3]
Intersection: [3, 1]
- Difference between the two sets
To perform the difference between two sets, the subtracting() method is used.
// First set
let setA: Set = [2, 3, 5]
print("Set A: ", setA)
// Second set
let setB: Set = [1, 2, 6]
print("Set B: ", setB)
// Perform subtraction operation
print("Subtraction: ", setA.subtracting(setB))
Output:
Set A: [3, 5, 2]
Set B: [1, 6, 2]
Subtraction: [3, 5]
To check if one set is a subset of another or not, the Subset() method is used.
// First set
let setA: Set = [1, 2, 3, 5, 4]
print("Set A: ", setA)
// Second set
let setB: Set = [1, 2]
print("Set B: ", setB)
// Check if setB is subset of setA or not
print("Subset: ", setB.isSubset(of: setA))
Output:
Set A: [3, 1, 2, 5]
Set B: [1, 2]
Subset: true
- Check if two sets are equal
We can check if two sets are equal by using the equality operator.
let setA: Set = [1, 3, 5]
let setB: Set = [3, 5, 1]
if setA == setB {
print("Set A and Set B are equal")
}
else {
print("Set A and Set B are different")
}
Output:
Set A and Set B are equal
Frequently Asked Questions
Are there sets in swift?
Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values.
What is the difference between a swift array and swift sets?
The main difference between the Swift array and Swift sets is that the Swift set cannot store the same value twice unlike the Swift array.
What is hashable swift?
Hashable swift means it must provide a hash value property. A hashValue is nothing but an integer that is the same for any two instances that compare equally.
What are swift sets?
Swift sets are used to store different values of the same types but they are unordered.
How do we declare a swift set?
We can declare a set that can store some values.
let someIntSet:Set = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(someIntSet)
Conclusion
In this article, we have extensively discussed swift sets and the various Swift set operation with the help of examples.
After reading about swift closures, are you not feeling excited to read/explore more articles on the topic of Swift? Don’t worry; Coding Ninjas has covered you. To learn, see Swift Methods, Swift Initialization, Timers in Swift, Swift Inheritance, and Swift Generics.
Refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithms, Competitive Programming, Javascript, System Design, and many more if you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!