Complete the code to find the union of two sets.
let setA: Set<Int> = [1, 2, 3] let setB: Set<Int> = [3, 4, 5] let unionSet = setA.[1](setB) print(unionSet.sorted())
The union method returns a new set with all elements from both sets.
Complete the code to find the intersection of two sets.
let setX: Set<String> = ["apple", "banana", "cherry"] let setY: Set<String> = ["banana", "date", "fig"] let commonFruits = setX.[1](setY) print(commonFruits.sorted())
The intersection method returns a new set with elements common to both sets.
Fix the error in the code to find the difference between two sets.
let set1: Set<Int> = [10, 20, 30, 40] let set2: Set<Int> = [30, 40, 50] let diffSet = set1.[1](set2) print(diffSet.sorted())
The correct method to find elements in set1 not in set2 is subtracting.
Fill both blanks to create a set of fruits that are in setA but not in setB.
let setA: Set<String> = ["orange", "apple", "banana"] let setB: Set<String> = ["banana", "kiwi"] let result = setA.[1](setB) print(result.[2]())
subtracting returns elements in setA not in setB. sorted() sorts the result in ascending order.
Fill all three blanks to create a dictionary with fruit names as keys (uppercase) and their counts as values, only for fruits with count greater than 1.
let fruits = ["apple", "banana", "apple", "cherry", "banana", "banana"] let counts = Dictionary(grouping: fruits, by: [1]).mapValues { $0.count } let filtered = counts.filter { $0.value [2] 1 } let result = filtered.mapKeys { [3] } print(result)
Grouping by the fruit name itself ({ $0 }), filtering counts greater than 1, and mapping keys to uppercase ({ $0.uppercased() }) creates the desired dictionary.