distinctBy

inline fun <T, K> Array<out T>.distinctBy(selector: (T) -> K): List<T>(source)

Returns a list containing only elements from the given array having distinct keys returned by the given selector function.

Among elements of the given array with equal keys, only the first one will be present in the resulting list. The elements in the resulting list are in the same order as they were in the source array.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
println(list.distinct()) // [a, A, b, B]
println(list.distinctBy { it.uppercaseChar() }) // [a, b] 
   //sampleEnd
}

inline fun <K> ByteArray.distinctBy(selector: (Byte) -> K): List<Byte>(source)
inline fun <K> ShortArray.distinctBy(selector: (Short) -> K): List<Short>(source)
inline fun <K> IntArray.distinctBy(selector: (Int) -> K): List<Int>(source)
inline fun <K> LongArray.distinctBy(selector: (Long) -> K): List<Long>(source)
inline fun <K> FloatArray.distinctBy(selector: (Float) -> K): List<Float>(source)
inline fun <K> DoubleArray.distinctBy(selector: (Double) -> K): List<Double>(source)
inline fun <K> BooleanArray.distinctBy(selector: (Boolean) -> K): List<Boolean>(source)
inline fun <K> CharArray.distinctBy(selector: (Char) -> K): List<Char>(source)

Returns a list containing only elements from the given array having distinct keys returned by the given selector function.

The elements in the resulting list are in the same order as they were in the source array.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
println(list.distinct()) // [a, A, b, B]
println(list.distinctBy { it.uppercaseChar() }) // [a, b] 
   //sampleEnd
}

inline fun <T, K> Iterable<T>.distinctBy(selector: (T) -> K): List<T>(source)

Returns a list containing only elements from the given collection having distinct keys returned by the given selector function.

Among elements of the given collection with equal keys, only the first one will be present in the resulting list. The elements in the resulting list are in the same order as they were in the source collection.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
println(list.distinct()) // [a, A, b, B]
println(list.distinctBy { it.uppercaseChar() }) // [a, b] 
   //sampleEnd
}