Home > Web Front-end > JS Tutorial > body text

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

PHP中文网
Release: 2016-05-16 15:02:59
Original
1355 people have browsed it



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


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)!


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