# Remove duplicates from an array in Kotlin

Hi, I am Amit Shekhar, Co-Founder @ [Outcome School](https://outcomeschool.com) • 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](https://outcomeschool.com/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.

Master Kotlin Coroutines from here: [Mastering Kotlin Coroutines](https://outcomeschool.com/blog/kotlin-coroutines)

That's it for now.

Thanks

**Amit Shekhar**

Co-Founder @ [Outcome School](https://outcomeschool.com)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)

[**Read all of our blogs here.**](https://outcomeschool.com/blog)
