1<script type="Text/javascript">2 varstr ='Qwertyuilo.,mnbvcsarrrrrrrrtyuiop;l,mhgfdqrtyuio;. Cvxsrtyiuo';3 varJSON = {};4 //Traverse str to disassemble each of these characters to take out the value of a character and the number of occurrences as a JSON kv5 for(vari =0; i < str.length; i++) {6 //determine if there is a value for the current STR in the JSON7 if(!Json[str.charat (i)]) {8 //Add the current value to the JSON if it does not exist9Json[str.charat (i)] =1;Ten}Else { One //else, the index value of the current value in the array is the + +; AJson[str.charat (i)]++; - } - } the //Store The most occurrences of the value and number of times - varNumber ="'; - varnum=0; - //traversing JSON using the DA Lei algorithm to count the required values + for(varIinchJSON) { - //if the current item is greater than the next item + if(json[i]>num) { A //change the current value to the maximum number of occurrences atnum =Json[i]; -Number =i; - } - } - //maximum number of values and occurrences of final printing -Alert'the most value that appears is'+number+'The number of occurrences is'+num); in -</script>
The note is still quite clear the pure hand dozen, the mistake please raise. Salute Silent Classmate!
The number of characters and occurrences of the most occurrences in a string are judged in JS