Skip to content
tezvyn:

Swift Collections: Array, Set, Dictionary

MediumHow cards are made

Swift's three collections are different access patterns, not just different APIs. Arrays keep order, Sets enforce uniqueness, and Dictionaries map keys to values. Picking an Array for uniqueness checks turns membership from O(1) into O(n) scans.

Why it exists

Before Swift codified its collection architecture, developers shoehorned every problem into arrays. This created linear searches, duplicates, and fragile parallel-array logic. Swift formalized three distinct types so your data structure matches your access pattern, making performance obvious at the declaration site.

The mental model

Think of the three collections as different filing systems. An Array is a numbered stack where position matters and duplicates are allowed. A Set is a velvet bag where you only care if an item is inside, not how many times or in what order. A Dictionary is a labeled cabinet where every document has a unique tag and you retrieve it by that tag. The choice is about which question you are asking: what is at index five, is this item present, or what belongs to this key?

How it works

Arrays store elements in contiguous memory with integer indices, giving O(1) random access but O(n) membership checks. Sets are backed by hash tables, so insertion, deletion, and containment are O(1) on average, but elements must be Hashable and order is undefined. Dictionaries also use hash tables, mapping unique Hashable keys to values for O(1) average lookup. All three are value types, so copies are cheap via copy-on-write, yet mutations trigger unique references before modification.

When to use it

Use an Array to preserve sequence, allow duplicates, or access elements by integer position, such as table view rows. Use a Set to enforce uniqueness, test membership, or perform operations like union and intersection, such as tracking selected tags. Use a Dictionary for associative lookup, such as caching objects by ID or grouping items by category.

When not to use it

Do not use an Array when you only care about uniqueness, because contains becomes a linear search. Do not use a Set when order matters or elements lack reliable Hashable conformance. Do not use a Dictionary when keys are unstable or you need positional sequencing, since dictionaries do not guarantee order. Avoid nesting optionals inside dictionaries unless you must distinguish a missing key from a nil value.

One canonical example

Imagine fetching user permissions from a server as an array of strings. Storing them in an Array means scanning every element to check for admin access. Store them in a Set instead, making permission checks O(1) while silently deduplicating malformed payloads. If you later need to map each permission to an expiration timestamp, upgrade to a Dictionary keyed by permission name. This mirrors the natural shift from ordered lists to uniqueness checks to associative metadata.

Interview question

You switch from a Set to an Array to store user permissions. What causes the slower membership checks?

  • a.Arrays use non-contiguous storage, forcing scattered memory reads during traversal
  • b.Copy-on-write copies the entire collection during each contains call, wasting CPU cycles
  • c.Arrays require Hashable conformance, adding overhead to every equality check
  • d.Arrays scan every element, turning contains into an O(n) operation instead of O(1)Correct
Why?

An Array performs a linear scan for contains, making membership O(n) rather than a Set's O(1) hash lookup. Copy-on-write does not trigger during read-only operations like contains, so it cannot explain the slowdown.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.

See open roles