Collection mutability tied to let/var in Swift - Time & Space Complexity
When we use collections like arrays or dictionaries in Swift, whether we declare them with let or var affects if we can change them. This impacts how operations on these collections behave over time.
We want to understand how the choice of let or var affects the time it takes to perform operations on collections.
Analyze the time complexity of the following Swift code snippet.
var numbers = [1, 2, 3, 4, 5]
numbers.append(6) // Allowed because 'numbers' is a var
let fixedNumbers = [1, 2, 3, 4, 5]
// fixedNumbers.append(6) // Error: Cannot modify because 'fixedNumbers' is a let
for number in numbers {
print(number)
}
This code shows how declaring a collection with var allows changes like adding items, while let makes the collection fixed and unchangeable.
Look at the parts that repeat work.
- Primary operation: Looping through the array with
for number in numbers. - How many times: Once for each item in the array, so as many times as the array's size.
As the array gets bigger, the loop runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times |
| 100 | 100 times |
| 1000 | 1000 times |
Pattern observation: The number of operations grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to loop through the collection grows in a straight line with the number of items.
[X] Wrong: "Declaring a collection with let means the loop will run faster because the collection can't change."
[OK] Correct: The loop runs the same number of times no matter if the collection is fixed or changeable. The let only stops changes, it doesn't speed up reading items.
Understanding how let and var affect collections helps you explain your code choices clearly. It shows you know how data changes impact performance, a useful skill in real projects.
What if we changed the for loop to a recursive function that processes each item? How would the time complexity change?
Practice
let, what does it mean for that collection?Solution
Step 1: Understand
letin Swiftletcreates a constant, meaning the value cannot be changed after it is set.Step 2: Apply to collections
If a collection is declared withlet, you cannot add, remove, or modify its elements because the collection itself is constant.Final Answer:
The collection cannot be changed after creation. -> Option AQuick Check:
letmeans constant collection [OK]
- Thinking you can add items to a let collection
- Confusing let with var for collections
- Assuming only items can change but not size
Solution
Step 1: Recall Swift syntax for mutable collections
Usevarto declare variables that can change.Step 2: Check options
var numbers = [1, 2, 3]correctly declares a mutable array.letis immutable, andconstormutableare not Swift keywords.Final Answer:
var numbers = [1, 2, 3] -> Option DQuick Check:
Mutable array uses var [OK]
- Using let instead of var for mutable collections
- Using non-Swift keywords like const or mutable
- Confusing var with let
let fruits = ["Apple", "Banana"]
fruits.append("Cherry")
print(fruits)Solution
Step 1: Identify collection mutability
The arrayfruitsis declared withlet, making it immutable.Step 2: Analyze append operation
Trying to append to an immutable array causes a compile-time error because you cannot modify a constant collection.Final Answer:
Compile-time error -> Option AQuick Check:
Append on let collection = Compile-time error [OK]
- Assuming append works on let collections
- Thinking it crashes at runtime instead of compile-time
- Expecting original array unchanged
let numbers = [1, 2, 3] numbers[0] = 10 print(numbers)
What is the problem here?
Solution
Step 1: Check array declaration
The arraynumbersis declared withlet, so it is immutable.Step 2: Understand element assignment
Trying to assign a new value to an element of an immutable array is not allowed and causes a compile-time error.Final Answer:
You cannot change elements of an array declared with let. -> Option BQuick Check:
Changing element in let array = Not allowed [OK]
- Thinking only size is fixed but elements can change
- Assuming index 0 is invalid
- Believing print causes error
Solution
Step 1: Understand mutability of dictionaries
To modify a dictionary after creation, it must be declared mutable.Step 2: Choose correct keyword
vardeclares a mutable dictionary allowing adding or changing keys and values.letmakes it immutable.Final Answer:
Use var because it allows modifying the dictionary after creation. -> Option CQuick Check:
Mutable dictionary needs var [OK]
- Using let and expecting to modify dictionary
- Thinking let allows changing keys or values
- Believing var prevents changing keys or values
Ready to prove it?
Pass the test and this pattern moves unknown
