1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <script> 9 var a= 'aaaaasssssddddddddds'; 10 /*建立一个哈希表,类似去重*/ 11 var json = {}; 12 for (var i = 0; i < a.length; i++) { 13 if (!json[a[i]]) { 14 json[a[i]] = 1; 15 }else{ 16 json[a[i]] ++; 17 } 18 }; 19 20 /*声明一个0;循环遍历哈希表,大于iMax,赋值并取出i值*/ 21 var iMax = 0; 22 var iIndex = ''; 23 for(var i in json){ 24 if (json[i]>iMax) { 25 iMax = json[i]; 26 iIndex =i; 27 }; 28 } 29 console.log(iMax+"xx"+iIndex) 30 </script> 31 </body> 32 </html>