Skip to content

Instantly share code, notes, and snippets.

@lukeduncan-scot
Last active May 24, 2021 22:23
Show Gist options
  • Select an option

  • Save lukeduncan-scot/02b5098ac73cf7248a29e557b7b934ab to your computer and use it in GitHub Desktop.

Select an option

Save lukeduncan-scot/02b5098ac73cf7248a29e557b7b934ab to your computer and use it in GitHub Desktop.
Counts Pairs in Iterable & Sequence
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