Sling Academy
Home/Kotlin/Removing Duplicate Elements from a List in Kotlin

Removing Duplicate Elements from a List in Kotlin

Last updated: December 05, 2024

Removing duplicate elements from a list is a common operation that many developers need to perform. In Kotlin, a modern JVM-based language, this task can be accomplished in various ways by using its rich standard library functions. In this article, we will explore different techniques for removing duplicates from a list in Kotlin with practical examples.

Using distinct() Function

Kotlin provides a straightforward way to remove duplicates from a list using the distinct() function. This function returns a list containing distinct elements only, in the order of their first occurrence.


fun main() {
    val numbers = listOf(1, 2, 3, 2, 4, 3, 5)
    val distinctNumbers = numbers.distinct()
    println(distinctNumbers) // Output: [1, 2, 3, 4, 5]
}

In this example, distinct() traverses the original list and filters out duplicate values, resulting in a new list with only unique elements.

Using toSet() and toList()

Another way to remove duplicates is by converting the list to a Set, which inherently does not allow any duplicate elements, and then back to a List.


fun main() {
    val names = listOf("Alice", "Bob", "Alice", "Charlie")
    val uniqueNames = names.toSet().toList()
    println(uniqueNames) // Output: [Alice, Bob, Charlie]
}

While this method effectively removes duplicates, it does not guarantee the preservation of the original order of elements.

Using distinctBy()

If you need to remove duplicates based on specific criteria, you can use distinctBy(). This function allows you to define a selector function to determine uniqueness.


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

fun main() {
    val people = listOf(
        Person("Alice", 25),
        Person("Bob", 22),
        Person("Alice", 30)
    )
    val distinctPeople = people.distinctBy { it.name }
    println(distinctPeople) // Output: [Person(name=Alice, age=25), Person(name=Bob, age=22)]
}

In this case, distinctBy() removes duplicates based on the name property of the Person class, allowing only the first occurrence of each name to be added to the result list.

Custom Implementation

For educational purposes or specific requirements, you might want to implement your method for removing duplicates using a loop and a MutableSet to track seen elements.


fun  List.removeDuplicatesCustom(): List {
    val seen = mutableSetOf()
    val result = mutableListOf()
    for (item in this) {
        if (seen.add(item)) { // add returns true if element was not present
            result.add(item)
        }
    }
    return result
}

fun main() {
    val letters = listOf("a", "b", "a", "c", "b", "d", "a")
    val noDuplicates = letters.removeDuplicatesCustom()
    println(noDuplicates) // Output: [a, b, c, d]
}

This custom function provides flexibility and understanding of how duplicate elements can be manually removed from a list. It uses a MutableSet to track unique elements and appends only new elements to the result list.

Conclusion

Kotlin's robust standard library offers multiple approaches to remove duplicates from a list effectively. Whether you're looking for a concise, out-of-the-box solution using the distinct() method or want more control using distinctBy() and custom solutions, Kotlin accommodates many needs. Understanding these techniques is crucial for writing clean and efficient Kotlin code.

Next Article: Partitioning Lists into Groups in Kotlin

Previous Article: Calculating the Sum, Average, and Median of List Elements in Kotlin

Series: Kotlin Collections

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin