DEV Community

loizenai
loizenai

Posted on

Kotlin Distinct() methods of List collection example

https://grokonez.com/kotlin/kotlin-distinct-methods-list-collection

Kotlin Distinct() methods of List collection example

In the tutorial, Grokonez will show you how to work with Kotlin distinct() method of List collection.

I. Practice

1. distinct() method

distinct() method is used to return a list that contains only distinct elements.

Method signature:

public fun <T> Iterable<T>.distinct(): List<T>
  • Work with simple list:
val simpleList = listOf(1, 2, 3, 5, 3, 4, 1, 10, 13, 7, 10, 8)
println(simpleList)
// print on console
//->
/*
    [1, 2, 3, 5, 3, 4, 1, 10, 13, 7, 10, 8]
*/

println(simpleList.distinct())
// print on console
// ->
/*
    [1, 2, 3, 5, 4, 10, 13, 7, 8]
*/
  • Work with Kotlin objects list:

More at:

https://grokonez.com/kotlin/kotlin-distinct-methods-list-collection

Kotlin Distinct() methods of List collection example

Top comments (0)