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
1317 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:
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template