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.

1
Q

What does an “array” do?

A

Stores values of the same type in an ordered list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a “set” do?

A

It stores distinct values of the same type in a collection with no defined ordering.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When is a good time to use a “set” instead of an array? (List two examples)

A
  1. When the order of items is not important

2. When you need to ensure that an item appears only once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What is a hash value?

2. Provide an example

A
  1. An Int value that is the same for all objects that compare equally.
  2. (EX.) - if a == b, the hash value of a is equal to the hash value of b
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A type must be _____ in order to be stored in a set.

A

hashable (The type must provide a way to compute a “hash value” for itself)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What Swift types are hashable by default? (List 2)

A
  1. All of Swift’s basic types such as String, Int, Double, and Bool.
  2. Enumeration case values without associated values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Data types that are hashable can be used as 1. ____ or 2. ______.

A
  1. Set value types

2. Dictionary key types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create an empty set of a certain type using the initializer syntax

A

var setName = Set( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Initialize a set with an array literal

A

var arrayLiteralSet: Set = [“ItemOne”, “ItemTwo”, “ItemThree”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Initialize a set with an array literal using Swift’s type inference to assume the type

A
var arrayLiteralSet: Set = ["ItemOne", "ItemTwo", "ItemThree"]
//Because all values are same type swift can infer that Set is correct.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

You access and modify a set through it’s 1)_____ and 2)_____.

A
  1. Methods

2. Properties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

You can add a new item into a set by calling the set’s ______ method.

A

insert(_: )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

You can check whether a sets count property is equal to 0 by using the ___ property.

A

isEmpty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Use isEmpty in an if statement

A
if setName.isEmpty {
instructions if true
} else {
instructions if false
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

You can remove an item from a set by calling the set’s _____ method

A

remove(_: )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

All items in a set can be removed with its _____ method.

A

removeAll( )

17
Q

To check whether a set contains a particular item, use the _____ method.

A

contains(_: )

18
Q

You can iterate over the values in a set with a _____ loop

19
Q

Swift’s set type does not have ____ _____.

A

Defined ordering

20
Q

To iterate over the values of a set in a specific order, use the _____ method.

21
Q

What will happen if you use a sets sorted( ) method on it?

A

It will return the set’s elements as an array sorted using the < operator.

22
Q

Use the 1. _____ method to create a new set with only the values common to both sets.

A
  1. intersection(_: )
23
Q

Use the 1. ______ method to create a new set with values in either set, but not both.

A
  1. symmetricDifference(_: )
24
Q

Use the 1. ______ method to create a new set with all the values in both sets

A
  1. union(_: )
25
Use the 1. _______ method to create a new set with values not in the specified set.
1. subtracting(_: )
26
Use the _____ operator to determine whether two sets contain all of the same values
"is equal" (==)
27
Use the ______ method to determine whether all of the values of a set are contained in a specific set.
isSubset(of: )
28
Use the _______ method to determine whether a set contains all of the values in a specified set.
isSuperset(of: )
29
Use the 1. ______ or 2. ______ methods to determine whether a set is a subset or superset, but not equal to, a specified set.
1. isStrictSubset(of: ) | 2. isStrictSuperset(of: )
30
Use the ______ method to determine whether two sets have no values in common.
isDisjoint(with: )
31
Why would set "a" be a "superset" of set "b"?
Because set "a" contains all elements in set "b"
32
Why would set "b" be a "subset" of set "a"?
Because all elements in set "b" are also contained in set "a"
33
Why would set "b" and set "c" be "disjoint" with one another?
Because they share no elements in common.