0Pricing
Swift Academy · Lesson

Choosing Between Array, Dictionary and Set

Performance characteristics and the right collection type for each scenario.

Choosing Between Array, Dictionary and Set is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Swift's three built-in collections — Array, Dictionary, and Set — each have distinct strengths. Choosing the right one impacts both correctness and performance.

Array: Ordered Sequences

Use `Array` when: • Order matters • You need random index access `O(1)` • Duplicates are allowed • You iterate sequentially ```swift let steps = ["boil","add tea","steep","drink"] ```

Dictionary: Key-Value Lookup

Use `Dictionary` when: • You need O(1) lookup by a unique key • Data is naturally keyed (user ID, config key) ```swift var userNames: [Int: String] = [42: "Alice", 7: "Bob"] print(userNames[42]!) // "Alice" ```

Set: Unique Membership

Use `Set` when: • You need O(1) membership testing • Duplicates must be excluded • Order doesn't matter ```swift var visitedPages: Set = [] visitedPages.insert("/home") print(visitedPages.contains("/home")) // true ```

Performance Summary

| Operation | Array | Dictionary | Set | |-------------------|-------|------------|-------| | Append | O(1)* | O(1)* | O(1)* | | Index/Key access | O(1) | O(1)* | — | | Search | O(n) | O(1)* | O(1)* | | Insert middle | O(n) | O(1)* | O(1)* | *amortised average

When Array Beats Set

Small collections often favour `Array` over `Set` due to lower overhead: ```swift // For 5 items, Array.contains is perfectly fast: let tags = ["swift","ios","mobile"] if tags.contains("swift") { ... } ``` The fixed overhead of hashing can dominate for tiny sets.

Ordered Dictionary Pattern

When you need key lookup AND insertion order: ```swift // Use an array of tuples or a struct: var ordered: [(key: String, value: Int)] = [] // Or Foundation's NSOrderedDictionary // Or maintain a [Key] array alongside a [Key:Value] dict ```

Multiset Pattern

For counted duplicates (multiset), use `[T: Int]`: ```swift var frequency: [String: Int] = [:] for word in words { frequency[word, default: 0] += 1 } ```

Immutability: let vs var

All three collections benefit from `let` for immutability: ```swift let config: [String: String] = ["env": "prod"] let primes: Set = [2,3,5,7,11] let steps: [String] = ["fetch","parse","display"] ``` Immutable collections avoid accidental mutation and enable compiler optimisations.

Practical Decision Tree

Ask: 1. Do I need ordered access by position? → Array 2. Do I need lookup by a unique key? → Dictionary 3. Do I need fast membership testing with no duplicates? → Set 4. Combination? → Compose: e.g. `[Key: [Value]]` for grouped data

Quick Check

Which collection provides O(1) average lookup by a unique key?

Recap

Key takeaways: • Array — ordered, indexed, allows duplicates • Dictionary — key-value pairs, O(1) lookup • Set — unique values, O(1) membership • Small collections: Array often wins over Set due to hashing overhead • Compose collections for complex data shapes Course complete! Next: struct design patterns.

Frequently asked questions

Is the “Choosing Between Array, Dictionary and Set” lesson free?

Yes — the full text of “Choosing Between Array, Dictionary and Set” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Choosing Between Array, Dictionary and Set”?

Performance characteristics and the right collection type for each scenario. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Choosing Between Array, Dictionary and Set” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Dictionary CRUD and Subscript Access
  2. Iterating and Transforming Dictionaries
  3. Set Operations: Union, Intersection, Difference
  4. Choosing Between Array, Dictionary and Set
← Back to Swift Academy