求字母出现的最大次数

javascript实现

function getMaxCharNum(str) {
    var arr = str.split(''),hash = {},max = 0,maxItem;
    arr.forEach(function(item){
        if(hash[item]){
            hash[item]++;
        }
        else{
            hash[item]=1;
        }
        if(max < hash[item]){
            max = hash[item];
            maxItem = item;
        }
    });
    return {
        count:max,
        item:maxItem,
        hash:hash
    }
}

 

posted @ 2016-12-20 13:22  全玉  阅读(211)  评论(0编辑  收藏  举报