0
0
Swiftprogramming~5 mins

Set algebra (union, intersection, difference) in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the union of two sets in Swift?
The union of two sets is a new set containing all unique elements from both sets combined.
Click to reveal answer
beginner
How do you find the intersection of two sets in Swift?
The intersection of two sets is a new set containing only the elements that appear in both sets.
Click to reveal answer
beginner
What does the difference of two sets represent?
The difference of two sets is a new set containing elements that are in the first set but not in the second set.
Click to reveal answer
beginner
Swift code: What does this do? <br><pre>let a: Set&lt;Int&gt; = [1, 2, 3]<br>let b: Set&lt;Int&gt; = [3, 4, 5]<br>let result = a.union(b)</pre>
It creates a new set with all unique elements from sets a and b: {1, 2, 3, 4, 5}.
Click to reveal answer
beginner
Swift code: What is the result of this? <br><pre>let a: Set&lt;Int&gt; = [1, 2, 3, 4]<br>let b: Set&lt;Int&gt; = [3, 4, 5]<br>let result = a.subtracting(b)</pre>
It creates a new set with elements in a but not in b: {1, 2}.
Click to reveal answer
Which Swift method returns elements common to both sets?
Aunion()
Bsubtracting()
Cintersection()
Dadding()
What does the union() method do in Swift sets?
AReturns elements only in the first set
BReturns all unique elements from both sets
CReturns elements only in the second set
DRemoves elements from the first set
If you want elements in set A but not in set B, which method do you use?
AsymmetricDifference()
Bintersection()
Cunion()
Dsubtracting()
What is the result of Set([1, 2, 3]).intersection(Set([3, 4, 5])) in Swift?
A{3}
B{1, 2}
C{4, 5}
D{1, 2, 3, 4, 5}
Which method would you use to find elements in either set but not in both?
AsymmetricDifference()
Bintersection()
Csubtracting()
Dunion()
Explain how to use union, intersection, and difference with Swift sets.
Think about combining, overlapping, and excluding elements.
You got /3 concepts.
    Describe a real-life example where set difference is useful.
    Imagine two lists and what is unique to one.
    You got /3 concepts.