本文主要是介绍字符串中出现次数最多的字符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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)
这篇关于字符串中出现次数最多的字符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!