Labs ICT
Pro Login

Arrays, Dictionaries, and Sets

Storing groups of values.

Arrays, Dictionaries, and Sets

Swift gives you three main collection types to store groups of data. Arrays are ordered lists—you can create them with type annotations like [String] or Array<String>.

var fruits = ["Apple", "Banana", "Orange"]
fruits.append("Mango")
fruits.remove(at: 1)
print(fruits.contains("Apple")) // true

Dictionaries store key-value pairs. Think of them like a real dictionary—look up a word (key) to find its definition (value). Accessing a dictionary returns an optional because the key might not exist:

var scores = ["Alice": 95, "Bob": 87, "Charlie": 92]
if let aliceScore = scores["Alice"] {
    print("Alice scored \(aliceScore)")
}

Sets are unordered collections of unique values. They're perfect when you need to ensure no duplicates. Create them with Set<Type> syntax:

var uniqueNumbers: Set<Int> = [1, 2, 3, 2, 1]
print(uniqueNumbers) // [1, 2, 3]

Arrays come with powerful methods like filter and map for transforming data. These functional programming tools make your code more expressive and concise.

Try it Yourself ->

🧪 Quick Quiz

What does a dictionary return when you access a missing key?