Home > Web Front-end > JS Tutorial > 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

一个新手
Release: 2017-09-07 10:03:41
Original
1287 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template