The 'distinctBy' function is a powerful tool for filtering a collection to contain only unique elements based on a specified selector. This can be particularly useful when you have a collection with duplicate entries, and you want to retain only one instance of each unique item based on a specific property.
val people = listOf(
Person("Alice", 30),
Person("Bob", 25),
Person("Alice", 35),
Person("Charlie", 25)
)
val uniquePeople = people.distinctBy { it.name }
uniquePeople.forEach { println(it) }