Back to questions
JuniorTheoryCommon
What does the program print?
let pack = [4, 7, 4, 9, 7]
var album = Set(pack)
album.insert(9)
album.insert(12)
print(pack.count, album.count)5 4
- Expecting
5 5: thinkinginsertalways adds, likeappend. A set skips a value it already holds, so inserting9again changes nothing. - Expecting
5 2: reading "no repeats" as "appears once".Set(pack)keeps every distinct value once, so4and7stay too.
- What does
print(album.sorted())print here? - Why can
print(album)list the elements in a different order on each run?