其他分享
首页 > 其他分享> > 字符串中出现次数最多的字符

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

作者:互联网

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)

标签:字符,muli,maxMuli,strArr,maxLetter,次数,let,letter,字符串
来源: https://www.cnblogs.com/zhongfang/p/14863842.html