Skip to content

Instantly share code, notes, and snippets.

@indrih17
Created February 25, 2021 09:54
Show Gist options
  • Save indrih17/adfa7cb7bc4093906b727d1bea5d00f0 to your computer and use it in GitHub Desktop.
Save indrih17/adfa7cb7bc4093906b727d1bea5d00f0 to your computer and use it in GitHub Desktop.
Set add instead of contains
fun <T> List<T>.hasDuplicates(): Boolean {
val accumulator = mutableSetOf<T>()
return any { !accumulator.add(it) }
}
fun main() {
println(listOf(1, 2, 5).hasDuplicates())
println(listOf(-5, 8, 0, 8).hasDuplicates())
println(emptyList<Int>().hasDuplicates())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment