ForeveHG / Frontend-Daily-Interview

学习,尝试回答一些前端面试题
1 stars 0 forks source link

93. 计算出字符串中出现次数最多的字符是什么?出现了多少次? #94

Open ForeveHG opened 3 years ago

ForeveHG commented 3 years ago
function findMaxCountStr(str) {
    let chartOfCount = {};
    while(str) {
        let chart = str.substr(0,1);
        if(chartOfCount[chart]) {
            chartOfCount[chart]++;
        } else {
            chartOfCount[chart] = 1;
        }
        str = str.substr(1)
    }
    return Object.keys(chartOfCount).map(chart => {
        return {
            chart: chart,
            count: chartOfCount[chart]
        }
    }).sort((a,b) => b.count - a.count)[0]
}

//测试

findMaxCountStr("aaaaaabbccccccccccc") // {chart: "c", count: 11}