Home  >  Article  >  Web Front-end  >  The character that appears most often in a string. Count this number [implementation code]_javascript skills

The character that appears most often in a string. Count this number [implementation code]_javascript skills

PHP中文网
PHP中文网Original
2016-05-16 15:02:591355browse



The most frequent characters in a string count this number [Implementation code]_javascript skills

var str = 'asdfssaaasasasasaa';
var json = {};

for (var i = 0; i < str.length; i++) {
  if(!json[str.charAt(i)]){
    json[str.charAt(i)] = 1;
  }else{
    json[str.charAt(i)]++;
  }
};
var iMax = 0;
var iIndex = &#39;&#39;;
for(var i in json){
  if(json[i]>iMax){
    iMax = json[i];
    iIndex = i;
  }
}
alert(&#39;出现次数最多的是:&#39;+iIndex+&#39;出现&#39;+iMax+&#39;次&#39;);


The above statistics of the most frequent characters in a string [implementation code] are all the content shared by the editor. For more related content, please pay attention to PHP Chinese website (www.php.cn)!


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