Swift Academy · Lesson

Choosing Between Array, Dictionary and Set

Performance characteristics and the right collection type for each scenario.

Lesson 4 of 412 steps

Choosing Between Array, Dictionary and Set is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

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

Yes — any 3 lessons of the Swift Academy learning path, including “Choosing Between Array, Dictionary and Set”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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