Set creation (setOf, mutableSetOf) in Kotlin - Time & Space Complexity
When we create sets in Kotlin, it's important to know how the time to build them grows as we add more items.
We want to understand how the work changes when the number of elements increases.
Analyze the time complexity of the following code snippet.
val numbers = listOf(1, 2, 3, 4, 5)
val set1 = setOf(1, 2, 3, 4, 5)
val mutableSet = mutableSetOf()
for (num in numbers) {
mutableSet.add(num)
}
This code creates a read-only set directly and a mutable set by adding elements one by one.
- Primary operation: Adding each element to the mutable set inside the loop.
- How many times: Once for each element in the input list.
As the number of elements increases, the number of add operations grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 add operations |
| 100 | About 100 add operations |
| 1000 | About 1000 add operations |
Pattern observation: The work grows in a straight line with the number of elements.
Time Complexity: O(n)
This means the time to create the set grows directly in proportion to the number of items you add.
[X] Wrong: "Adding elements to a set takes the same time no matter how many items there are."
[OK] Correct: Each element must be processed once, so more items mean more work, even if each add is fast.
Understanding how set creation scales helps you explain efficiency when working with collections in real projects.
"What if we used a nested loop to add elements from two lists into a set? How would the time complexity change?"
Practice
setOf(1, 2, 2, 3) return?Solution
Step 1: Understand
setOfbehaviorsetOfcreates a set that holds only unique elements, automatically removing duplicates.Step 2: Analyze the input elements
The input has elements 1, 2, 2, 3. The duplicate 2 is ignored in the set.Final Answer:
A set containing unique elements: 1, 2, 3 -> Option CQuick Check:
setOf removes duplicates = unique elements [OK]
- Thinking sets keep duplicates like lists
- Confusing setOf with listOf
- Expecting an error for duplicates
Solution
Step 1: Identify correct syntax for mutableSetOf
The correct function ismutableSetOfwith type parameter and parentheses.Step 2: Check each option
val mySet = mutableSetOf<String>("apple", "banana") usesmutableSetOf<String>("apple", "banana"), which is correct. val mySet = setOf<String>("apple", "banana") usessetOfwhich creates an immutable set. val mySet = mutableSet("apple", "banana") uses a non-existent functionmutableSet. val mySet = mutableSetOf("apple", "banana") has a syntax error with an extra quote.Final Answer:
val mySet = mutableSetOf<String>("apple", "banana") -> Option AQuick Check:
mutableSetOf syntax correct = val mySet = mutableSetOf<String>("apple", "banana") [OK]
- Using setOf instead of mutableSetOf for mutable sets
- Misspelling mutableSetOf
- Syntax errors with quotes or parentheses
val numbers = mutableSetOf(1, 2, 3) numbers.add(2) numbers.add(4) println(numbers)
Solution
Step 1: Understand mutableSetOf and add behavior
The set starts with 1, 2, 3. Adding 2 again does nothing because sets ignore duplicates. Adding 4 adds a new element.Step 2: Determine final set contents
After additions, the set contains 1, 2, 3, and 4.Final Answer:
[1, 2, 3, 4] -> Option DQuick Check:
Sets ignore duplicates, so 2 is not added twice [OK]
- Expecting duplicates to appear in output
- Confusing set print format with list
- Thinking add returns error on duplicates
val fruits = setOf("apple", "banana")
fruits.add("orange")
println(fruits)Solution
Step 1: Check mutability of setOf
setOfcreates an immutable set that cannot be changed after creation.Step 2: Analyze the add operation
Callingaddon an immutable set causes a compilation error because the set cannot be modified.Final Answer:
Cannot add elements to an immutable set -> Option BQuick Check:
setOf = immutable, add() not allowed [OK]
- Trying to modify immutable sets
- Confusing setOf with mutableSetOf
- Ignoring compiler error messages
Solution
Step 1: Understand filtering and set creation
We want a mutable set with only numbers > 5. UsingfilterTowith a mutable set collects filtered items directly into a mutable set.Step 2: Evaluate each option
val nums = mutableSetOf(1, 6, 8, 3, 7).filter { it > 5 } returns a List, not a mutable set. val nums = mutableSetOf(1, 6, 8, 3, 7).filterTo(mutableSetOf()) { it > 5 } correctly filters into a mutable set. val nums = setOf(1, 6, 8, 3, 7).filter { it > 5 }.toSet() creates an immutable set first, then filters to list and toSet(), producing an immutable set, not mutable. val nums = mutableSetOf(1, 6, 8, 3, 7).filter { it > 5 }.toSet() filters to a list then toSet(), producing an immutable set, not mutable.Final Answer:
val nums = mutableSetOf(1, 6, 8, 3, 7).filterTo(mutableSetOf()) { it > 5 } -> Option AQuick Check:
filterTo with mutableSetOf creates mutable set directly [OK]
- Using filter() returns list, not set
- Not converting to mutable set after filtering
- Confusing setOf and mutableSetOf usage
Ready to prove it?
Pass the test and this pattern moves unknown
