DEV Community

Cover image for Remove duplicates from an array in Kotlin
Amit Shekhar
Amit Shekhar

Posted on • Edited on • Originally published at outcomeschool.com

Remove duplicates from an array in Kotlin

Hi, I am Amit Shekhar, Co-Founder @ Outcome School • IIT 2010-14 • 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.

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.

This article was originally published at Outcome School.

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)
Enter fullscreen mode Exit fullscreen mode

And, an array of Mentor:

val mentors = arrayOf(
    Mentor(1, "Amit Shekhar"),
    Mentor(2, "Anand Gaurav"),
    Mentor(1, "Amit Shekhar"),
    Mentor(3, "Lionel Messi"))
Enter fullscreen mode Exit fullscreen mode

Remove duplicates using distinct()

In Kotlin, we can use distinct() function available in Collection functions to remove duplicates.

val distinct = mentors.distinct()
println(distinct)
Enter fullscreen mode Exit fullscreen mode

This will print the following:

[Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav),
Mentor(id=3, name=Lionel Messi)]
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

This will print the following:

[Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav),
Mentor(id=3, name=Lionel Messi)]
Enter fullscreen mode Exit fullscreen mode

Note:

  • Maintain the original order of items.
  • Returns Set which is a read-only set. It means that we can't perform operations like add on the set. Next, we will see toMutableSet() which returns read/write set.

Remove duplicates using toMutableSet()

In Kotlin, we can use toMutableSet() function available in Collection functions to remove duplicates.

val toMutableSet = mentors.toMutableSet()
println(toMutableSet)
Enter fullscreen mode Exit fullscreen mode

This will print the following:

[Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav),
Mentor(id=3, name=Lionel Messi)]
Enter fullscreen mode Exit fullscreen mode

Note:

  • Maintain the original order of items.
  • Returns MutableSet which is a read/write set. It means that we can perform operations like add on the mutable set.

Remove duplicates using toHashSet()

In Kotlin, we can use toHashSet() function available in Collection functions to remove duplicates.

val toHashSet = mentors.toHashSet()
println(toHashSet)
Enter fullscreen mode Exit fullscreen mode

This will print the following:

[Mentor(id=3, name=Lionel Messi),
Mentor(id=1, name=Amit Shekhar),
Mentor(id=2, name=Anand Gaurav)]
Enter fullscreen mode Exit fullscreen mode

Note:

  • Similar to MutableSet but 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.

That's it for now.

Thanks

Amit Shekhar

Co-Founder @ Outcome School

You can connect with me on:

Read all of our blogs here.

Top comments (0)