W3cubDocs

/Kotlin

distinctBy

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K> Array<out T>.distinctBy(
    selector: (T) -> K
): List<T>

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.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
println(list.distinct()) // [a, A, b, B]
println(list.distinctBy { it.toUpperCase() }) // [a, b]
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <K> ByteArray.distinctBy(
    selector: (Byte) -> K
): List<Byte>
inline fun <K> ShortArray.distinctBy(
    selector: (Short) -> K
): List<Short>
inline fun <K> IntArray.distinctBy(
    selector: (Int) -> K
): List<Int>
inline fun <K> LongArray.distinctBy(
    selector: (Long) -> K
): List<Long>
inline fun <K> FloatArray.distinctBy(
    selector: (Float) -> K
): List<Float>
inline fun <K> DoubleArray.distinctBy(
    selector: (Double) -> K
): List<Double>
inline fun <K> BooleanArray.distinctBy(
    selector: (Boolean) -> K
): List<Boolean>
inline fun <K> CharArray.distinctBy(
    selector: (Char) -> K
): List<Char>

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.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
println(list.distinct()) // [a, A, b, B]
println(list.distinctBy { it.toUpperCase() }) // [a, b]
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K> Iterable<T>.distinctBy(
    selector: (T) -> K
): List<T>

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.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
println(list.distinct()) // [a, A, b, B]
println(list.distinctBy { it.toUpperCase() }) // [a, b]
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/distinct-by.html