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<Int> = [1, 2, 3]<br>let b: Set<Int> = [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<Int> = [1, 2, 3, 4]<br>let b: Set<Int> = [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?
✗ Incorrect
The intersection() method returns elements that appear in both sets.
What does the union() method do in Swift sets?
✗ Incorrect
union() combines all unique elements from both sets.
If you want elements in set A but not in set B, which method do you use?
✗ Incorrect
subtracting() returns elements in the first set that are not in the second.
What is the result of Set([1, 2, 3]).intersection(Set([3, 4, 5])) in Swift?
✗ Incorrect
Only 3 is common to both sets.
Which method would you use to find elements in either set but not in both?
✗ Incorrect
symmetricDifference() returns elements in either set but not in both.
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.