Simple example of judging the most common characters in a string and the number of occurrences in JS
<script type="text/javascript"> var str = 'qwertyuilo.,mnbvcsarrrrrrrrrrrrrrrrrrrrrrr;l,mhgfdqrtyuio;.cvxsrtyiuo'; var json = {}; //Transfuse str to tear each character and take out the value of a certain character and the number of occurrences as json kv for (var i = 0; i < str.length; i++) { //Judge whether there is the current value in json if (!json[str.charAt(i)]) { //If it does not exist, add the current value to json to json[str.charAt(i)] = 1; } else { //If you else, let the index value of the current value already in the array++; json[str.charAt(i)]++; } } //Storage the value and number of occurrences var number = ''; var num=0; //Transaction json Use the challenge algorithm to count the required values for (var i in json) { //If the current item is greater than the next item if (json[i]>num) { //Change the current value to the value with the most occurrences num = json[i]; number = i; } } //Finally print the value with the most occurrences and the number of occurrences alert('The value with most occurrences is '+number+'The number of occurrences is '+num); </script>The above simple example of judging the most common characters and the number of occurrences in a string in JS is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.