Search code examples
arraysscalaapache-sparkapache-spark-sqlmode

Statistical Mode of an array in Scala


I need to find Statistical mode of an array. Consider

val data = Array(101, 100, 101, 100, 100, 100, 103, 100, 100, 100, 101, 101, 150, 150, 150, 151, 150, 150, 150, 150)

[Above 'data' array is sample one.] Actually array contains 200 values for which I need to find Statistical mode value (common most occurred value) for every consecutive 10 data points using Scala.

Any idea that would work is appreciated.


Solution

  • data.grouped(10).map(_.groupBy(identity).mapValues(_.size).maxBy(_._2)._1).toList