Skip to content

Instantly share code, notes, and snippets.

@solidsnack
Created July 30, 2017 18:24
Show Gist options
  • Save solidsnack/39daed3e99975e1cb09df8e9dabf6f3a to your computer and use it in GitHub Desktop.
Save solidsnack/39daed3e99975e1cb09df8e9dabf6f3a to your computer and use it in GitHub Desktop.
fun median(data: List<Long>): Long {
val sorted = data.sorted()
// NB: When data.size is odd, we have: i == j
val i = sorted.size / 2
val j = (sorted.size - 1) / 2
val meanOfMiddle = (sorted[j] + sorted[i]) / 2.0
return meanOfMiddle.toLong()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment