Next Q: Is there a more idiomatic way to find firs...
# getting-started
a
Next Q: Is there a more idiomatic way to find first duplicate in a List What I have creates a
mutableList
of counts and then picks the first one by `count > 1`:
Copy code
fun <E> List<E>.findFirstDuplicate(): E? {
    val counts = mutableMapOf<E, Int>()
    this.forEach {
        if (counts.containsKey(it))
            counts.put(it, counts[it]!! + 1)
        else
            counts.put(it, 1)
    }

    return this.find { counts[it]!! > 1 }
}