Find the most frequent character in a string I'm trying to solve this problem https://community.topcoder.com/stat?c=problem_statement&amp;pm=40


  Given a string made up of ONLY letters and
      digits, determine which character is repeated the most in the string ('A' is
      different than 'a'). If there is a tie, the character which appears first in
      the string (from left to right) should be returned.


Here is my approach is Scala. I feel it's not very functional, and slow(because of a new reversed String object). 

def findMaxFunctional(word: String): Char = {
    var curMaxCount, maxCharId = 0
    word.reverse.foldLeft(new Array[Int](128))((acc, ch) =&gt; {
      acc(ch) = acc(ch) + 1
      if(acc(ch) &gt;= curMaxCount) {
        curMaxCount = acc(ch)
        maxCharId = ch
      }
      acc
    })
    maxCharId.toChar
  }
Instead of reversing the input string you could foldRight over it, and instead of updating mutable variables let the fold accumulator carry the current data forward. As an Array is mutable by default there's no point in having the fold accumulator make copies in order to carry it forward.

def findMaxFunctional(word: String): Char = {
  val arr = new Array[Int](128)
  word.foldRight((' ', 0)){
    case (c, (mxChr, mxCnt)) =&gt;
      arr(c) += 1
      if (arr(c) &gt;= mxCnt) (c, arr(c))
      else (mxChr, mxCnt)
  }._1
}