Swift Sets Flashcards
Gain a comprehensive understanding of how to work with the properties and methods of Sets in swift to improve ability to successfully complete toy problems.
What does an “array” do?
Stores values of the same type in an ordered list.
What does a “set” do?
It stores distinct values of the same type in a collection with no defined ordering.
When is a good time to use a “set” instead of an array? (List two examples)
- When the order of items is not important
2. When you need to ensure that an item appears only once.
- What is a hash value?
2. Provide an example
- An Int value that is the same for all objects that compare equally.
- (EX.) - if a == b, the hash value of a is equal to the hash value of b
A type must be _____ in order to be stored in a set.
hashable (The type must provide a way to compute a “hash value” for itself)
What Swift types are hashable by default? (List 2)
- All of Swift’s basic types such as String, Int, Double, and Bool.
- Enumeration case values without associated values.
Data types that are hashable can be used as 1. ____ or 2. ______.
- Set value types
2. Dictionary key types.
Create an empty set of a certain type using the initializer syntax
var setName = Set( )
Initialize a set with an array literal
var arrayLiteralSet: Set = [“ItemOne”, “ItemTwo”, “ItemThree”]
Initialize a set with an array literal using Swift’s type inference to assume the type
var arrayLiteralSet: Set = ["ItemOne", "ItemTwo", "ItemThree"] //Because all values are same type swift can infer that Set is correct.
You access and modify a set through it’s 1)_____ and 2)_____.
- Methods
2. Properties
You can add a new item into a set by calling the set’s ______ method.
insert(_: )
You can check whether a sets count property is equal to 0 by using the ___ property.
isEmpty
Use isEmpty in an if statement
if setName.isEmpty { instructions if true } else { instructions if false }
You can remove an item from a set by calling the set’s _____ method
remove(_: )
All items in a set can be removed with its _____ method.
removeAll( )
To check whether a set contains a particular item, use the _____ method.
contains(_: )
You can iterate over the values in a set with a _____ loop
for-in
Swift’s set type does not have ____ _____.
Defined ordering
To iterate over the values of a set in a specific order, use the _____ method.
sorted( )
What will happen if you use a sets sorted( ) method on it?
It will return the set’s elements as an array sorted using the < operator.
Use the 1. _____ method to create a new set with only the values common to both sets.
- intersection(_: )
Use the 1. ______ method to create a new set with values in either set, but not both.
- symmetricDifference(_: )
Use the 1. ______ method to create a new set with all the values in both sets
- union(_: )
Use the 1. _______ method to create a new set with values not in the specified set.
- subtracting(_: )
Use the _____ operator to determine whether two sets contain all of the same values
“is equal” (==)
Use the ______ method to determine whether all of the values of a set are contained in a specific set.
isSubset(of: )
Use the _______ method to determine whether a set contains all of the values in a specified set.
isSuperset(of: )
Use the 1. ______ or 2. ______ methods to determine whether a set is a subset or superset, but not equal to, a specified set.
- isStrictSubset(of: )
2. isStrictSuperset(of: )
Use the ______ method to determine whether two sets have no values in common.
isDisjoint(with: )
Why would set “a” be a “superset” of set “b”?
Because set “a” contains all elements in set “b”
Why would set “b” be a “subset” of set “a”?
Because all elements in set “b” are also contained in set “a”
Why would set “b” and set “c” be “disjoint” with one another?
Because they share no elements in common.