Last active
May 24, 2021 22:23
-
-
Save LDuncAndroid/02b5098ac73cf7248a29e557b7b934ab to your computer and use it in GitHub Desktop.
Counts Pairs in Iterable & Sequence
This file contains hidden or 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> Iterable<T>.countPairs() = groupingBy { it } | |
.eachCount() | |
.values | |
.sumOf { it.div(2) } | |
fun <T> Sequence<T>.countPairs() = groupBy { it } | |
.mapValues { it.value.size } | |
.values | |
.sumOf {it.div(2)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment