Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert list of integers to a map with frequency per bin?

Lets say I have a list of numbers:

val numbers = List(15, 30, 110, 140, 170, 210)

How can I count the number of integers per bin of a 100 in order to get:

Map(0 -> 2, 100 -> 3, 200 -> 1)
like image 263
WillamS Avatar asked Feb 15 '23 11:02

WillamS


1 Answers

// val items = List(1, 2, 3, 101, 330, 302)
items.groupBy(i => i/100).map { case (i, l) => (i*100, l.length) }
// Map(100 -> 1, 300 -> 2, 0 -> 3)
like image 160
Marth Avatar answered Feb 16 '23 23:02

Marth