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

let str = 'hellochinahellochinahelloj'
  let strArr = str.split('').sort()
  console.log(strArr)
  let muli = ''
  let letter = 1
  let maxMuli = strArr[0]
  let maxLetter = 1
  for (let i = 1; i < strArr.length; i++) {
    if(strArr[i-1] === strArr[i]) {
      letter = letter + 1
      muli = strArr[i]
    } else{
      if(maxLetter < letter) {
        maxLetter = letter
        maxMuli = muli
        
      }
      letter = 1
    }
  }
  console.log('maxMuli', maxMuli)
  console.log('maxLetter', maxLetter)
posted @ 2021-06-08 17:48  zhongfang99  阅读(126)  评论(0编辑  收藏  举报