0
0
Swiftprogramming~5 mins

Set creation and operations in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you create an empty set of integers in Swift?
Use var numbers = Set<Int>() to create an empty set of integers.
Click to reveal answer
beginner
What is the difference between a Set and an Array in Swift?
A Set stores unique values with no defined order, while an Array stores ordered values and can have duplicates.
Click to reveal answer
beginner
How do you add an element to a Swift set?
Use the insert() method, for example: mySet.insert(5) adds the number 5 to the set.
Click to reveal answer
intermediate
What does the union() operation do on two Swift sets?
It combines all unique elements from both sets into a new set, without duplicates.
Click to reveal answer
intermediate
How can you find common elements between two sets in Swift?
Use the intersection() method to get a new set with elements found in both sets.
Click to reveal answer
Which Swift code creates a set of strings?
Avar fruits = ["apple", "banana"]
Bvar fruits = {"apple", "banana"}
Cvar fruits = ("apple", "banana")
Dvar fruits: Set<String> = ["apple", "banana"]
What happens if you insert a duplicate element into a Swift set?
AThe set ignores the duplicate and remains unchanged.
BThe set converts to an array.
CThe set throws a runtime error.
DThe set adds the duplicate element again.
Which method returns elements in one set but not in another?
Asubtracting()
Bintersection()
Cunion()
Dinsert()
How do you check if a Swift set contains a specific element?
Aset.has(element)
Bset.includes(element)
Cset.contains(element)
Dset.find(element)
What is the result of setA.union(setB)?
AA set with elements only in setB
BA set with all unique elements from setA and setB
CA set with elements only in setA
DA set with elements common to setA and setB
Explain how to create a set in Swift and add elements to it.
Think about starting empty and then putting things inside.
You got /3 concepts.
    Describe the difference between union, intersection, and subtracting operations on sets.
    Imagine mixing, matching, and removing items from two baskets.
    You got /3 concepts.