Kotlin get unique elements from list

Kotlin get unique elements from list

1. Overview

In this article, we will learn to get unique elements from the Kotlin list.

Kotlin provides the standard library functions: distinct and distinctBy to get unique elements from a list or array.

2. Kotlin get unique elements from list

Now, let’s see examples to get unique elements from an array or list.

2.1. Kotlin distinct to get unique elements

You can use the distinct function to get a list of distinct elements from an array or list. The elements in the output list would be in the same order as they were in the source array.

For example, the fruitList contains a array of fruits and has duplicate entries. Therefore, you can call the distinct function to get unique fruits. The output of this function is a list.

fun main() {
    val fruitList = arrayOf("Apple", "Banana", "Banana", "Citrus", "Apple", "Guava", "Fig")
    val outputList = fruitList.distinct()
    println(outputList) 
    println(outputList is List) 
}

If you desire to get the output as an array instead of a list, you can call toTypedArray function to convert the list to an array.

fun main() {
    val fruitList = arrayOf("Apple", "Banana", "Banana", "Citrus", "Apple", "Guava", "Fig")
    val fruitArray = fruitList.distinct().toTypedArray()
    println(fruitArray) 
    println(fruitArray is Array) 
}

2.2. Kotlin distinct by to get unique elements

Kotlin distinct by function allows specifying selector to distinguish the elements in an array and returns accordingly.

For example, the below code contains distinct By function that takes a selector which uses length as a key.

fun main() {
    val fruitList = arrayOf("Apple", "Banana", "Banana", "Citrus", "Apple", "Guava", "Fig")
    val outputList = fruitList.distinctBy {
        it.length
    }
    println(outputList) 
}

This distinct by function is way more effective than distinct.

Assume, you have a list of Employee objects with member variables name and id. Both Kyle and Mike have the same id. You can use the distinct by function to remove the duplicates using the id variable.

You cannot use the normal distinct function in this scenario as it treats both Kyle and Mike as distinct values.

fun main() {
    val emp1 = Employee(213, "John")
    val emp2 = Employee(214, "Afra")
    val emp3 = Employee(215, "Mike")
    val emp4 = Employee(214, "Kyle")
    val employeeList = listOf(emp1, emp2, emp3, emp4)
    println(employeeList)
    val outputList = employeeList.distinctBy {
        it.id
    }
    println(outputList) 
    outputList = employeeList.distinct()
    println(outputList) 
}
data class Employee(val id: Long, val name: String)

2.2.1. Kotlin distinct by multiple fields

In the previous section, we have seen to get distinct elements from the Employee list by using a single member variable id.

Assume, now we also have deptId in the employee object. What if you want to filter based on employee id and also deptId.

You can use pass the Kotlin Pair as selector to the distinct By function. This will make the distinct by function to filter using both member variables:

fun main() {
    val emp1 = Employee(213, "John", 78)
    val emp2 = Employee(214, "Afra", 72)
    val emp3 = Employee(215, "Mike", 75)
    val emp4 = Employee(214, "Kyle", 72)
    val employeeList = listOf(emp1, emp2, emp3, emp4)
    println(employeeList)
    val outputList = employeeList.distinctBy {
        Pair(it.id, it.deptId)
    }
    println(outputList) 
}
data class Employee(val id: Long, val name: String, val deptId: Long)

Alternatively, you can create a data class with required fields id and deptId and pass it as the selector. This would allow you to specify any number of fields as a condition for filtering the unique elements.

fun main() {
    val emp1 = Employee(213, "John", 78)
    val emp2 = Employee(214, "Afra", 72)
    val emp3 = Employee(215, "Mike", 75)
    val emp4 = Employee(214, "Kyle", 72)
    val employeeList = listOf(emp1, emp2, emp3, emp4)
    println(employeeList)
    val outputList = employeeList.distinctBy {
        Selector(it.id, it.deptId)
    }
    println(outputList) 
}
data class Selector(val id: Long, val deptId: Long)
data class Employee(val id: Long, val name: String, val deptId: Long)

3. Conclusion

To sum up, we have learned to get the unique elements from the Kotlin array or list using the distinct and distinct by functions.

Leave a Comment