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