Created
February 25, 2021 09:54
-
-
Save indrih17/adfa7cb7bc4093906b727d1bea5d00f0 to your computer and use it in GitHub Desktop.
Set add instead of contains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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