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!