Array, Set, and Dictionary
  • July 23, 2025

In Swift (on iOS or anywhere else), Array, Set, and Dictionary are all value-type collections (structs) with different guarantees:


1. Array<Element>

  • Ordered, indexed, allows duplicates.
  • Random access by integer index (O(1)).
  • Literal syntax: let nums = [1, 2, 3]
  • Common ops:
    var a = [1, 2, 3]
    a.append(4)
    let first = a[0]
    a.remove(at: 1)
    
  • Use when order matters or you need positions (e.g., table view data).

2. Set<Element> (where Element: Hashable)

  • Unordered, unique elements (no duplicates).
  • Fast membership tests/inserts/removals (O(1) average).
  • Literal via initializer: let primes: Set<Int> = [2, 3, 5, 7]
  • Useful set algebra:
    let a: Set = [1, 2, 3]
    let b: Set = [3, 4, 5]
    a.union(b)            // {1,2,3,4,5}
    a.intersection(b)     // {3}
    a.subtracting(b)      // {1,2}
    a.isSuperset(of: [1]) // true
    
  • Use when you only care whether something is present, not its position or duplicates.

3. Dictionary<Key: Hashable, Value>

  • Key–value lookup table. Keys are unique and hashable.
  • Average O(1) lookup/insert/remove by key.
  • Literal syntax: let ages = ["Alice": 30, "Bob": 27]
  • Common ops:
    var dict = ["score": 10]
    dict["score"] = 20           // update
    dict["level"] = 3            // insert
    let s = dict["score"]        // optional Int?
    dict.removeValue(forKey: "score")
    dict.updateValue(99, forKey: "bonus") // returns old value if any
    
  • Use when you need to associate values with identifiers (IDs, names, enums, etc.).

Quick chooser

Need… Pick
Ordered list, duplicates, index access Array
Fast “is this here?” checks, no duplicates Set
Map keys to values Dictionary

Shared traits & tips

  • Value semantics: Assigning or passing them copies the value. Swift uses copy-on-write, so it’s cheap until you mutate.
  • Generics & type inference: Compiler usually infers Element, Key, Value.
  • Mutability: let makes the whole collection immutable; use var to mutate.
  • Iteration order: Arrays keep order. Sets are conceptually unordered. Dictionaries maintain insertion order in current Swift, but don’t rely on it for logic.
  • Transforming:
    let arr = [1, 2, 3]
    let set = Set(arr)
    let dict = Dictionary(uniqueKeysWithValues: arr.map { ($0, "\($0)") })
    

That’s the core difference. Want examples for custom types, performance, or bridging with Objective-C? Just ask!

YOU MIGHT ALSO LIKE...
🧭 NavigationKit

NavigationKit is a lightweight library which makes SwiftUI navigation super easy to use. 💻 Installation 📦 Swift Package Manager Using Swift Package Manager, add ...

swiftui-navigation-stack

An alternative SwiftUI NavigationView implementing classic stack-based navigation giving also some more control on animations and programmatic navigation. NavigationStack Installation ...

Stinsen

Simple, powerful and elegant implementation of the Coordinator pattern in SwiftUI. Stinsen is written using 100% SwiftUI which makes it ...

SwiftUI Router

With SwiftUI Router you can power your SwiftUI app with path-based routing. By utilizing a path-based system, navigation in your app becomes ...

FlowStacks

This package takes SwiftUI's familiar and powerful NavigationStack API and gives it superpowers, allowing you to use the same API not just ...