Kotlin Program to Remove Duplicates from a List
You can remove duplicates in Kotlin by converting a list to a set using
toSet() or by using distinct() on the list, for example: val uniqueList = list.distinct().Examples
Input[1, 2, 2, 3, 4, 4, 5]
Output[1, 2, 3, 4, 5]
Input["apple", "banana", "apple", "orange"]
Output["apple", "banana", "orange"]
Input[]
Output[]
How to Think About It
To remove duplicates, think about keeping only one copy of each item. Since duplicates are repeated items, you can use a collection that automatically ignores repeats, like a set, or use a function that filters out repeated elements while keeping the original order.
Algorithm
1
Get the input list with possible duplicates.2
Use a method to filter out repeated items, such as converting to a set or using a distinct function.3
Return or print the new list with duplicates removed.Code
kotlin
fun main() {
val list = listOf(1, 2, 2, 3, 4, 4, 5)
val uniqueList = list.distinct()
println(uniqueList)
}Output
[1, 2, 3, 4, 5]
Dry Run
Let's trace the list [1, 2, 2, 3, 4, 4, 5] through the code using distinct()
1
Original list
[1, 2, 2, 3, 4, 4, 5]
2
Apply distinct()
Scans list and keeps first occurrence of each element
3
Resulting list
[1, 2, 3, 4, 5]
| Index | Element | Is Duplicate? | Action |
|---|---|---|---|
| 0 | 1 | No | Keep |
| 1 | 2 | No | Keep |
| 2 | 2 | Yes | Skip |
| 3 | 3 | No | Keep |
| 4 | 4 | No | Keep |
| 5 | 4 | Yes | Skip |
| 6 | 5 | No | Keep |
Why This Works
Step 1: Using distinct()
The distinct() function scans the list and keeps only the first occurrence of each element, removing duplicates.
Step 2: Preserving order
distinct() keeps the original order of elements while removing duplicates, unlike converting to a set which may reorder.
Step 3: Printing the result
The unique list is printed showing the list without duplicates.
Alternative Approaches
Using toSet()
kotlin
fun main() {
val list = listOf(1, 2, 2, 3, 4, 4, 5)
val uniqueSet = list.toSet()
println(uniqueSet)
}Converts list to a set which removes duplicates but does not guarantee order.
Using LinkedHashSet
kotlin
fun main() {
val list = listOf(1, 2, 2, 3, 4, 4, 5)
val uniqueSet = LinkedHashSet(list)
println(uniqueSet.toList())
}LinkedHashSet removes duplicates and preserves insertion order.
Complexity: O(n) time, O(n) space
Time Complexity
The distinct() function scans each element once, so it runs in linear time relative to the list size.
Space Complexity
It uses extra space to store unique elements, so space grows linearly with input size.
Which Approach is Fastest?
distinct() and toSet() both run in O(n), but distinct() preserves order, which is often preferred.
| Approach | Time | Space | Best For |
|---|---|---|---|
| distinct() | O(n) | O(n) | Removing duplicates while preserving order |
| toSet() | O(n) | O(n) | Removing duplicates when order does not matter |
| LinkedHashSet | O(n) | O(n) | Removing duplicates with order preservation and set behavior |
Use
distinct() to remove duplicates while keeping the original order of elements.Using
toSet() when order matters, as sets do not preserve the original list order.