js统计字符串,并且判断出现次数最多的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script> var s="123123189273893724901734891379486452098524345"; var fres={}; //用来记录所有的字符的出现频次 var mostFreqChar=''; //用来记录最大频次的字符 var key; for(var i=0;i<s.length;i++){ var char=s[i]; if(!fres[char]){ fres[char]=0; } fres[char]++; } for(key in fres){ if(mostFreqChar==''||fres[key]>fres[mostFreqChar]){ mostFreqChar=key; } } console.log(mostFreqChar); console.log(fres[mostFreqChar]); </script> </head> <body> </body> </html>
耐得住寂寞,守得住繁华