The number of characters and occurrences of the most occurrences in a string are judged in JS

Source: Internet
Author: User

  

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.