统计数组里出现次数最多的字母

1

const maxLetter = function(str) {
  const temp = {}
  const arr = str.split('')
  
  for(const item of arr) {
    if (item in temp) {
      temp[item]++
    } else {
      temp[item] = 1
    }
  }

  let max = 0
  for(const item in temp) {
    if(temp[item] > max) {
      max = temp[item]
    }
  }
  return max
}

console.log(maxLetter('alkfjdjlaaaaksjfnvcoiewoifsdksla'))

1

猜你喜欢

转载自blog.csdn.net/m0_38066007/article/details/121103238#comments_20226588