distinct

fun <T> Array<out T>.distinct(): List<T>

Returns a list containing only distinct elements from the given array.

Among equal elements of the given array, 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
assertPrints(list.distinct(), "[a, A, b, B]")
assertPrints(list.distinctBy { it.uppercaseChar() }, "[a, b]") 
   //sampleEnd
}

fun ByteArray.distinct(): List<Byte>
fun ShortArray.distinct(): List<Short>
fun IntArray.distinct(): List<Int>
fun LongArray.distinct(): List<Long>
fun FloatArray.distinct(): List<Float>
fun DoubleArray.distinct(): List<Double>
fun BooleanArray.distinct(): List<Boolean>
fun CharArray.distinct(): List<Char>

Returns a list containing only distinct elements from the given array.

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
assertPrints(list.distinct(), "[a, A, b, B]")
assertPrints(list.distinctBy { it.uppercaseChar() }, "[a, b]") 
   //sampleEnd
}

fun <T> Iterable<T>.distinct(): List<T>

Returns a list containing only distinct elements from the given collection.

Among equal elements of the given collection, 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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
assertPrints(list.distinct(), "[a, A, b, B]")
assertPrints(list.distinctBy { it.uppercaseChar() }, "[a, b]") 
   //sampleEnd
}