https://kotlinlang.org logo
#getting-started
Title
# getting-started
h

Hrodrick

10/02/2023, 1:57 PM
Hi everybody, are there any built-in collection functions for mean, median, and mode? https://www.dictionary.com/e/average-vs-mean-vs-median-vs-mode/ As I understand, we can use
.average()
in a list of Int which works as mean. But what about the others? In particular
.mode()
(I would prefer just
.mostFrequent()
) would be useful for any type of collection or at least List 🤔
c

Chris Fillmore

10/02/2023, 2:06 PM
There is a link here for a library called kotlin-statistics, but it 404’s https://kotlinlang.org/docs/data-science-overview.html#kotlin-libraries
e

ephemient

10/02/2023, 3:13 PM
median:
list.sorted()[list.length / 2]
(plus whatever logic you want to handle ties) mode:
list.groupingBy { it }.eachCount().entries.maxBy { it.value }.key
h

Hrodrick

10/02/2023, 3:14 PM
Yes I was using that. Will need to continue with it, thanks guys!