js-打印出现最多次的字母

Posted on 2019-08-26 00:17  牛顿8848  阅读(127)  评论(0编辑  收藏  举报
let str='asjfhsdgbmzxgaudsnfkjndgjkdsngnzbdsjkbgjkdsbg';
let wjy={},word;
// let len=str.length;
for(let i = 0;i<str.length;i++){
    word=str[i];
    if(wjy[word]){
        wjy[word]++
    }else{
        wjy[word]=1;
    }
}
var maxNum = 0;
let arr = [];
for(key in wjy){
    if(maxNum<wjy[key]){
        maxNum = wjy[key];
    }
}
for(key in wjy){
    if(wjy[key] == maxNum){
        arr.push(key)
    }
}
console.log(wjy)
console.log("出现最多的字母是"+ arr,"出现的次数为" + maxNum + "次")