Ever found yourself struggling to clean up a collection in Kotlin that has duplicates? 🤔 You’re not alone! Whether you’re working with lists or sets, ensuring that your collections contain only unique items can be tricky. In this article, I’ll show you some simple yet powerful techniques to deduplicate your collections in Kotlin, making your code cleaner and more efficient.
If you’re interested in diving deeper into Kotlin-related topics, consider reading my other articles like Kotlin Null Safety Explained or Kotlin 2.0 Updates for Android Developers.
Why Deduplicate Collections?
Imagine you’re managing a shopping cart for an e-commerce app. The user accidentally adds the same product multiple times, and suddenly, your cart is overflowing with duplicate items! 🛒 Instead of allowing this to happen, you can use Kotlin’s rich standard library to quickly remove duplicates and ensure your collections are unique.
Not only does this improve data integrity, but it also optimizes performance when dealing with large datasets. If you want to learn more about efficient data handling in Android, be sure to check out my guide on Understanding Mobile App Architecture.
How Duplicates Can Affect Your App’s Performance
Duplicates can significantly affect the performance of your app. When you have large collections with redundant data, your algorithms need to process more items than necessary, leading to higher computational costs and memory usage. By removing duplicates, you ensure that your data is not only accurate but also processed efficiently.
Method 1: Using toSet()
to Remove Duplicates
The easiest way to eliminate duplicates from a collection in Kotlin is to convert it to a Set. A set inherently contains only unique elements. Here’s how you can use the toSet()
function:
val items = listOf("apple", "banana", "apple", "orange", "banana")
val uniqueItems = items.toSet()
println(uniqueItems) // Output: [apple, banana, orange]
The resulting Set contains only unique items. Note that the toSet()
method does not preserve the order of elements, so if order matters, consider using the distinct()
function.
Learn More About Kotlin Collections
To understand more about how Kotlin handles collections and their methods, check out the official documentation on Kotlin Collections.
Method 2: Preserving Order with distinct()
If you need to remove duplicates while maintaining the order of the original list, distinct()
is the function you’re looking for. It filters out duplicates and returns a new list that keeps the order of the first occurrence of each item:

The distinct()
function is great when you want to remove duplicates without losing the sequence of elements, which is crucial for many data processing tasks. You can find more details about this function in the official documentation on Kotlin’s Distinct Function.
Method 3: Removing Duplicates Based on a Property
Sometimes, you may have a collection of objects where you want to remove duplicates based on a specific property. Let’s say you have a list of Person objects, and you want to ensure each person has a unique email address:
data class Person(val name: String, val email: String)
val people = listOf(
Person("Alice", "[email protected]"),
Person("Bob", "[email protected]"),
Person("Alice", "[email protected]"),
Person("Charlie", "[email protected]")
)
val uniqueEmails = people.distinctBy { it.email }
println(uniqueEmails)
// Output: [Person(name=Alice, [email protected]), Person(name=Bob, [email protected]), Person(name=Charlie, [email protected])]
The distinctBy
function lets you specify the property to deduplicate by, making it perfect for complex data structures. This is particularly useful when dealing with data models in apps, where you may need to filter lists of users, products, or other entities.
Dealing with Concurrency in Collections
If your collections are being modified concurrently, handling duplicates can be a bit trickier. You might want to look into Concurrency in Kotlin Using Coroutines to see how you can safely manipulate collections in concurrent environments.
Method 4: Using Mutable Collections
for In-Place Deduplication
If you’re working with mutable collections, you can remove duplicates in-place by using a HashSet to track seen elements:
val items = mutableListOf("apple", "banana", "apple", "orange", "banana")
val seen = HashSet<String>()
items.retainAll { seen.add(it) }
println(items) // Output: [apple, banana, orange]
This approach can be useful when you need to deduplicate without creating a new collection. However, it is slightly more verbose and may not be as intuitive as using toSet()
or distinct()
.
Exploring State Management in Android Compose
For those working on Android development, managing collections often comes into play when dealing with state. For more insights, you can read about State Management in Android Compose.
If you want to see how these concepts are applied practically in Android development using Jetpack Compose, refer to Jetpack Compose Official Documentation.
Conclusion
Removing duplicates from collections is a common problem, and Kotlin provides multiple ways to handle it effectively. Whether you choose to convert your list to a set, use the distinct()
function, or filter out duplicates based on a specific property, Kotlin makes it simple to clean up your collections.
Each method has its use case: use toSet()
for quick deduplication, distinct()
to maintain order, and distinctBy
for object properties. So next time you encounter duplicates in your data, you’ll know exactly how to tackle them!
If you’re eager to learn more advanced techniques and tools for Android development, check out my article on Building a Jetpack Compose Project from Scratch.
Did you like this article?
You can subscribe to my newsletter below and get updates about my new articles.