Created
July 30, 2017 18:24
-
-
Save solidsnack/39daed3e99975e1cb09df8e9dabf6f3a to your computer and use it in GitHub Desktop.
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 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