Remove duplicates from an array in Kotlin

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
Remove duplicates from an array in Kotlin

I am Amit Shekhar, Co-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.

Join Outcome School and get high paying tech job: Outcome School

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

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.

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)]

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)

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 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)

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 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.

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar
Co-Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.