ProgrammingJunior iOS Developer

What are the types of collections in Swift, how do they differ in internal implementation, and when should each collection be used?

Pass interviews with Hintsage AI assistant

Answer.

In Swift, there are three main standard collections: Array, Set, and Dictionary. Each of them implements different interfaces, differs in internal structure, and is intended for different tasks.

Background:

In Swift, collections are designed with a focus on type safety and performance, including value semantics and Copy-on-Write copying for Arrays/Sets/Dictionaries.

Problem:

Often, a beginner developer uses only Array where it would be more appropriate to use Set or Dictionary, which leads to memory overuse and reduced access/search speed. It's important to understand the differences.

Solution:

Array

An ordered collection of elements, indexed access, duplicates are allowed. Typically implemented with a dynamic buffer.

Set

An unordered collection of unique elements (Hashable). Access is by hash — very fast, duplicates are not allowed.

Dictionary

A collection of key-value pairs. The key must be Hashable. Access to the value by key (via hash table).

Example code:

var arr: [Int] = [1, 2, 3, 4] var set: Set<Int> = [1, 2, 2, 3] var dict: [String: Int] = ["a": 1, "b": 2] // set == [1, 2, 3] — duplicates are discarded

Key features:

  • Array is efficient for index access, maintains order, but access by value is linear
  • Set is fast for searching and checking the existence of an element, stores only unique Hashable values
  • Dictionary is optimal for key-based (Hashable) lookups, does not maintain order but quickly finds and modifies values

Trick questions.

Can non-Hashable types be used as elements of Set or Dictionary?

No. To be stored in Set/Dictionary, an element must be Hashable (have a unique hash identifier). Otherwise, the compiler will not allow the collection to be created.

struct Point {} // Set<Point> will cause an error because Point is not Hashable

Is the order of storage of elements in Set the same as when they are added?

No. Set does not guarantee order — iteration can provide any order. If you need order, use Array.

What happens when trying to access a non-existing key in Dictionary?

Dictionary will return an Optional. Be careful — trying to access a non-existing key will yield nil, not an error.

let val = dict["not exist"] // val — nil

Common mistakes and anti-patterns

  • Using Array to search for unique values (better to use Set)
  • Using Dictionary to store values without the need to search by key
  • Attempting to use non-Hashable types as keys/values

Real-life example

Negative case

A developer uses Array for a unique list of users and checks uniqueness through contains. On large lists, this takes a lot of time for checks.

Pros:

  • Simplicity of implementation

Cons:

  • Linear complexity of search, excessive storage of duplicates, high memory burden

Positive case

The same list is implemented as a Set. Checking for existence is instantaneous, unique values are guaranteed.

Pros:

  • Fast access, memory savings, duplicates eliminated

Cons:

  • No guarantees on the order of elements