1. Overview

In this short tutorial, we’ll learn how to remove duplicate values from an array in Kotlin.

2. The toMutableSet Extension Function

Let’s start by taking a basic scenario where we’ve got an array of strings with duplicate values, and we’re required to remove duplicate values from it.

Most programming languages provide the set data structure for storing unique values as a collection. Kotlin is no different, so let’s go ahead and use the toMutableSet() function to convert the array into a set:

val data = arrayOf("pie", "tie", "lie", "pie")
val uniqueData = data.toMutableSet()
assertEquals(3, uniqueData.size)
assertTrue(uniqueData.contains("pie"))
assertTrue(uniqueData.contains("tie"))
assertTrue(uniqueData.contains("lie"))

We can notice that the original array had 4 values, but the resultant, uniqueData set contains only unique values, and its size is 3.

3. The distinct Extension Function

Kotlin also provides the distinct extension function, which can be used to return distinct values of an array as a list:

val data = arrayOf("pie", "tie", "lie", "pie")
assertEquals(4, data.size)
val uniqueData = data.distinct()
assertEquals(3, uniqueData.size)
assertEquals("pie", uniqueData[0])
assertEquals("tie", uniqueData[1])
assertEquals("lie", uniqueData[2])

It’s important to note that the elements in the resultant list have the same order as in the original array. Amongst the duplicate values, only the first value is available.

4. The distinctBy Extension Function

In this section, we’ll learn how we can remove duplicate values from an array that holds entity objects.

4.1. Employee Data Class

Let’s start by defining the Employee data class:

data class Employee(val id: String, val name: String)

We must note that two employees can have the same name but not the same id.

4.2. Distinct Employees by Criteria

Entity objects can have multiple attributes. As a result, we can have multiple criteria to retrieve distinct values.

For such scenarios, Kotlin provides the distinctBy extension function, which we can use to specify criteria for removing duplicate values.

Let’s say we have got an array of employees with duplicate values for the id attribute:

val emp1 = Employee("Jimmy", "1")
val emp2 = Employee("James", "2")
val emp3 = Employee("Jimmy", "3")
val employees = arrayOf(emp1, emp2, emp1, emp3)

We can notice duplicate values for the emp3 entity object in the array. Now, let’s use the distinctBy function to get the employees with distinct id:

val uniqueEmployees = employees.distinctBy { it.id }
assertEquals(3, uniqueEmployees.size)
assertEquals("Jimmy", uniqueEmployees.get(0).name)
assertEquals("1", uniqueEmployees.get(0).id)
assertEquals("James", uniqueEmployees.get(1).name)
assertEquals("2", uniqueEmployees.get(1).id)
assertEquals("Jimmy", uniqueEmployees.get(2).name)
assertEquals("3", uniqueEmployees.get(2).id)

As expected, the resultant list contains only 3 employees.

Next, let’s take a scenario where we’re interested in getting the list of employees with unique names.

val emp1 = Employee("John", "1")
val emp2 = Employee("John", "2")
val employees = arrayOf(emp1, emp2, emp1)

We can notice that not only the original array has repeated values for emp1, but the emp2 entity also has the same name as emp1.

To get the employees with unique names, let’s use the distinctBy function using the name attribute:

val employeesWithUniqueNames = employees.distinctBy { it.name }
assertEquals(1, employeesWithUniqueNames.size)
assertEquals("John", employeesWithUniqueNames.get(0).name)
assertEquals("1", employeesWithUniqueNames.get(0).id)

As expected, the resultant list of employees has got a single employee.

5. Conclusion

In this tutorial, we focussed on writing idiomatic code in Kotlin to remove duplicate values and entity objects from an array.

As always, the code is available over on GitHub.