Swift collections: Array, Dictionary, and Set
Compare ordering, key-based lookup, and uniqueness, with examples of basic collection operations.
Read the Korean original on Velog ↗English translation

Swift's main collection types are Array, Dictionary, and Set. Choose based on whether order matters, values need keys, or duplicates should be excluded.
Array: an ordered collection
var numbers: [Int] = []
numbers.append(3)
numbers.append(10)
numbers[0] = 7
numbers.contains(10) // true
numbers.removeLast()
An array preserves order, supports integer indices, and allows duplicate values. Accessing an invalid index causes a runtime error.
Dictionary: keys and values
var words: [String: String] = [:]
words["elephant"] = "코끼리"
let value = words["elephant"] // String?
words.removeValue(forKey: "elephant")
A lookup returns an optional because the key may not be present.
Set: unique values
let a: Set<Int> = [1, 2, 3, 4]
let b: Set<Int> = [2, 4, 6]
a.union(b)
a.intersection(b)
a.subtracting(b)
A set does not guarantee order and does not store duplicates. Use an array when position matters, a dictionary for key-based lookup, and a set for uniqueness and membership checks.