To use foldLeft in what you want to do, and following your style, you can simply return the same drive array in the calculation, like this:
val ret = a.foldLeft(new Array[Int](10)) { (acc, c) => acc(c) += 1; acc }
Alternatively, since your numbers are from 0 to 9, you can also do this to achieve the same result:
val ret = (0 to 9).map(x => a.count(_ == x))
source share