Remove duplicates from an array in Kotlin
Tags: android, kotlin
Published: 2022-09-16 05:20:25

---
title: Remove duplicates from an array in Kotlin
published: true
description: 
tags: android, kotlin
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekhuc23jlhgslivhyxzl.png
canonical_url: https://amitshekhar.me/blog/remove-duplicates-from-an-array-in-kotlin
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

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 [amitshekhar.me](https://amitshekhar.me/blog/remove-duplicates-from-an-array-in-kotlin).**

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:

```kotlin
data class Mentor(val id: Int, val name: String)
```

And, an `array` of `Mentor`:

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

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

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

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

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

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)
- [Facebook](https://www.facebook.com/amit.shekhar.iitbhu)