Remove duplicates from an array in Kotlin
- Authors
- Name
- Amit Shekhar
- Published on
We can remove duplicates from an array in Kotlin using any of these functions: distinct(), toSet(), toMutableSet(), or toHashSet(), depending on our use case.
In this blog, we are going to learn how to remove duplicates from an array in Kotlin. As there are many ways to remove duplicates from an array in Kotlin, depending on the use case, we can decide which one to use.
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning, and Android at Outcome School.
Join Outcome School and get a high-paying tech job:
Let's get started.
We can use any function from the following to remove duplicates from an array in Kotlin:
distinct()toSet()toMutableSet()toHashSet()
Let's start learning one by one by example.
Consider a data class Mentor like below:
data class Mentor(val id: Int, val name: String)
And, an array of Mentor:
val mentors = arrayOf(
Mentor(1, "Amit Shekhar"),
Mentor(2, "Anand Gaurav"),
Mentor(1, "Amit Shekhar"),
Mentor(3, "Lionel Messi"))
Remove duplicates using distinct()
In Kotlin, we can use distinct() function available in Collection functions to remove duplicates.
val distinct = mentors.distinct()
println(distinct)
This will print the following:
[Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav),
Mentor(id=3, name=Lionel Messi)]
Note:
- Maintain the original order of items.
- Among equal elements of the given array, only the first one will be present in the output.
- Returns
List
Here, as we have used it to remove duplicate mentors from the array, similarly, we can use it to remove duplicate strings from an array.
Remove duplicates using toSet()
In Kotlin, we can use toSet() function available in Collection functions to remove duplicates.
val toSet = mentors.toSet()
println(toSet)
This will print the following:
[Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav),
Mentor(id=3, name=Lionel Messi)]
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
Note:
- Maintain the original order of items.
- Returns
Setwhich is aread-onlyset. It means that we can't perform operations likeaddon the set. Next, we will seetoMutableSet()which returnsread/writeset.
Remove duplicates using toMutableSet()
In Kotlin, we can use toMutableSet() function available in Collection functions to remove duplicates.
val toMutableSet = mentors.toMutableSet()
println(toMutableSet)
This will print the following:
[Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav),
Mentor(id=3, name=Lionel Messi)]
Note:
- Maintain the original order of items.
- Returns
MutableSetwhich is aread/writeset. It means that we can perform operations likeaddon the mutable set.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Remove duplicates using toHashSet()
In Kotlin, we can use toHashSet() function available in Collection functions to remove duplicates.
val toHashSet = mentors.toHashSet()
println(toHashSet)
This will print the following:
[Mentor(id=3, name=Lionel Messi),
Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav)]
Note:
- Similar to
MutableSetbut do NOT maintain the original order of items. - Returns
HashSet
Here, as we have used it to remove duplicate mentors from the array, similarly, we can use it to remove any duplicate elements like strings, numbers, etc from an array.
So, we understood how to remove duplicates from an array in Kotlin.
To master Android internals, check out the Android Program by Outcome School.
Frequently Asked Questions
Which function should we use to remove duplicates and keep the original order?
We can use distinct(), toSet(), or toMutableSet(), since all three maintain the original order of items. distinct() returns a List, toSet() returns a read-only Set, and toMutableSet() returns a read and write MutableSet.
Does toHashSet keep the original order of items?
No. toHashSet() is similar to a MutableSet, but it does not maintain the original order of items. It returns a HashSet, so the elements can come out in a different order than they were in the array.
Can we add items to the set returned by toSet?
No. toSet() returns a read-only Set, so we cannot perform operations like add on it. If we need to add items later, we should use toMutableSet(), which returns a read and write set.
Which element is kept when distinct finds duplicates?
Among equal elements of the array, only the first one will be present in the output of distinct(). The rest of the equal elements are dropped, and the result is returned as a List in the original order.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
