Kotlin Tutorials

Kotlin Set distinctBy()
Syntax & Examples

Set.distinctBy() extension function

The distinctBy() extension function for sets in Kotlin returns a list containing only elements from the set that have distinct keys returned by the given selector function.


Syntax of Set.distinctBy()

The syntax of Set.distinctBy() extension function is:

fun <T, K> Set<T>.distinctBy(selector: (T) -> K): List<T>

This distinctBy() extension function of Set returns a list containing only elements from the given set having distinct keys returned by the given selector function.

Parameters

ParameterOptional/RequiredDescription
selectorrequiredA function that takes an element of the set and returns a key used for determining distinct elements.

Return Type

Set.distinctBy() returns value of type List.



✐ Examples

1 Using distinctBy() to get a list of distinct elements by length

In Kotlin, we can use the distinctBy() function to get a list of distinct elements from a set of strings based on their length.

For example,

  1. Create a set of strings.
  2. Use the distinctBy() function with a selector function that returns the length of each string.
  3. Print the resulting list to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val fruits = setOf("apple", "banana", "cherry", "date")
    val distinctByLength = fruits.distinctBy { it.length }
    println("Distinct fruits by length: $distinctByLength")
}

Output

Distinct fruits by length: [apple, banana, date]

2 Using distinctBy() to get a list of distinct elements by first character

In Kotlin, we can use the distinctBy() function to get a list of distinct elements from a set of strings based on their first character.

For example,

  1. Create a set of strings.
  2. Use the distinctBy() function with a selector function that returns the first character of each string.
  3. Print the resulting list to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val words = setOf("apple", "apricot", "banana", "blueberry")
    val distinctByFirstChar = words.distinctBy { it.first() }
    println("Distinct words by first character: $distinctByFirstChar")
}

Output

Distinct words by first character: [apple, banana]

3 Using distinctBy() with an empty set

In Kotlin, we can use the distinctBy() function to get a list of distinct elements from an empty set based on a given selector function, which will result in an empty list.

For example,

  1. Create an empty set of integers.
  2. Use the distinctBy() function with a selector function that returns the integer itself.
  3. Print the resulting list to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val emptySet = emptySet<Int>()
    val distinctNumbers = emptySet.distinctBy { it }
    println("Distinct elements in empty set: $distinctNumbers")
}

Output

Distinct elements in empty set: []

Summary

In this Kotlin tutorial, we learned about distinctBy() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.