Kotlin Array distinct Function

The distinct function in Kotlin is used to return a list containing only distinct (unique) elements from an array. This function is part of the Kotlin standard library and provides a simple way to eliminate duplicate elements from an array.

Table of Contents

  1. Introduction
  2. distinct Function Syntax
  3. Understanding distinct
  4. Examples
    • Basic Usage
    • Using distinct with Custom Types
    • Combining distinct with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The distinct function removes duplicate elements from an array and returns a list containing only the unique elements. This is useful for filtering out duplicates and ensuring that a collection contains only distinct values.

distinct Function Syntax

The syntax for the distinct function is as follows:

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

Parameters:

  • This function does not take any parameters.

Returns:

  • A list containing the distinct elements of the original array.

Understanding distinct

The distinct function iterates through the array and collects only the unique elements, removing any duplicates. The result is a list that contains each unique element from the original array exactly once.

Examples

Basic Usage

To demonstrate the basic usage of distinct, we will create an array of integers with duplicate elements and filter out the duplicates.

Example

fun main() {
    val numbers = arrayOf(1, 2, 2, 3, 3, 3, 4, 5)
    val distinctNumbers = numbers.distinct()
    println("Original array: ${numbers.joinToString()}")
    println("Distinct list: ${distinctNumbers.joinToString()}")
}

Output:

Original array: 1, 2, 2, 3, 3, 3, 4, 5
Distinct list: 1, 2, 3, 4, 5

Using distinct with Custom Types

This example shows how to use distinct with an array of custom objects. For custom types, the equals and hashCode methods need to be properly overridden to ensure correct behavior.

Example

data class Person(val name: String, val age: Int)

fun main() {
    val people = arrayOf(
        Person("Ravi", 25),
        Person("Anjali", 30),
        Person("Priya", 22),
        Person("Ravi", 25)
    )

    val distinctPeople = people.distinct()
    println("Original array: ${people.joinToString()}")
    println("Distinct list: ${distinctPeople.joinToString()}")
}

Output:

Original array: Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22), Person(name='Ravi', age=25)
Distinct list: Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22)

Combining distinct with Other Functions

This example demonstrates how to combine distinct with other functions, such as filter, to perform more complex operations.

Example

fun main() {
    val numbers = arrayOf(1, 2, 2, 3, 3, 3, 4, 5)
    val distinctEvenNumbers = numbers.filter { it % 2 == 0 }.distinct()
    println("Distinct even numbers: ${distinctEvenNumbers.joinToString()}")
}

Output:

Distinct even numbers: 2, 4

Real-World Use Case

Filtering Unique Data Entries

In real-world applications, the distinct function can be used to filter out duplicate entries in datasets, such as ensuring that a list of users contains only unique entries.

Example

data class User(val id: Int, val name: String)

fun main() {
    val users = arrayOf(
        User(1, "Ravi"),
        User(2, "Anjali"),
        User(3, "Priya"),
        User(1, "Ravi")
    )

    val uniqueUsers = users.distinct()
    println("Unique users: ${uniqueUsers.joinToString()}")
}

Output:

Unique users: User(id=1, name='Ravi'), User(id=2, name='Anjali'), User(id=3, name='Priya')

Conclusion

The distinct function in Kotlin is a convenient method for filtering out duplicate elements from an array, returning a list containing only the unique elements. It provides a simple way to ensure that collections contain only distinct values. By understanding and using this function, you can effectively manage and filter unique elements in your Kotlin applications.

Comments