Hi all, any idea why this compiles:
fun <T, K> Collection<T>.getDuplicates(keySelector: (T) -> K): Set<T> =
if (this is Set) emptySet() else groupBy(keySelector).values.filter { it.size > 1 }.mapTo(mutableSetOf()) { it.first() }
but when I try to combine the filter
and mapTo
via mapNotNullTo
I get a “type mismatch: required any
but found T
”:
fun <T, K> Collection<T>.getDuplicates(keySelector: (T) -> K): Set<T> =
if (this is Set) emptySet() else groupBy(keySelector).values.mapNotNullTo(mutableSetOf()) { occurrences -> occurrences.first().takeIf { occurrences.size > 1 } }