Home  >  Article  >  Web Front-end  >  How to use js to know the most frequent characters and their times

How to use js to know the most frequent characters and their times

一个新手
一个新手Original
2017-09-07 10:03:411233browse

var getMax = (str) => {    
    var o = {};    
    for(var i in str){
        o[str[i]] ? o[str[i]]++ : o[str[i]] = 1; 
    }    
    var keys = Object.keys(o);    
    var values = Object.values(o);    
    var maxVal = Math.max.apply(Math,values);
    console.log(keys[values.indexOf(maxVal)],maxVal);
}
getMax('aaabbbbcddeefggggggggggghijjjggk') // g 13
getMax('aaabbbbcddeefggggggggggghijjjk') // g 11
getMax('aaabbggggggggbbcddeefggggggggggghijjjk') // g 19

The above is the detailed content of How to use js to know the most frequent characters and their times. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn