charleyup

为自己吹过的🐮🍺奋斗终生.

统计字符串中出现次数最多的字符

2017/12/31

经典算法题

const charStr = str => {
  const obj = {}
  let max = 0 // 出现最多的字符的出现次数
  let maxValue = null // 出现最多的字符
  str.split('').forEach(item => {
    obj[item] = obj[item] ? obj[item] + 1 : 1
    if (obj[item] > max) {
      max = obj[item]
      maxValue = item
    }
  })
  return maxValue
}