判断一个字符串中出现次数最多的字符

let a = "aaaaaaaaadsdfdfdfeeeeeeee";
let json = {}
for(let i=0;i<a.length;i++){
    if(!json[a[i]]){
        json[a[i]] =1
    }else{
        json[a[i]]++
    }
}
console.log(json)
let maxNum = 0
let maxStr;
for(let i in json){
    if(json[i]>maxNum){
        maxNum = json[i]
        maxStr = i
    }
}
console.log("出现次数最多的字符是"+maxStr+",出现了"+maxNum+"次")

判断一个字符串中出现次数最多的字符

猜你喜欢

转载自blog.51cto.com/13550695/2462774