To remove duplicates in a list in Kotlin, you can use the distinct method.
In this example,
numbers
with integer values.distinct
method on the list to remove duplicates.fun main() {
val numbers = listOf(1, 2, 2, 3, 4, 4, 5)
val distinctNumbers = numbers.distinct()
println("List without duplicates: $distinctNumbers")
}
List without duplicates: [1, 2, 3, 4, 5]
In this example,
words
with string values.distinct
method on the list to remove duplicates.fun main() {
val words = listOf("apple", "banana", "cherry", "apple", "banana")
val distinctWords = words.distinct()
println("List without duplicates: $distinctWords")
}
List without duplicates: [apple, banana, cherry]
In this tutorial, we learned How to Remove Duplicates in a List in Kotlin language with well detailed examples.