In a string, like 'zhaochucichuzuiduodezifu', we want to find out the characters that appear the most. This article will explain the methods and ideas in detail.
First introduce two methods in two string objects : indexOf() and charAt() methods
indexOf() method introduces the return of the first occurrence of a specified string value in the string
charAt() method introduces the return character at a specified position
First do a small test to find where each 'i' appears in the string 'woainixiaoli'.
<script>var arr = 'woainixiaoli';var index = -1; //Define the variable index to control the index value//When a cannot be found, that is, the value of indexOf() is -1, the loop ends do {index = arr.indexOf("i", index + 1); //Use the second parameter index+1 to control each lookup starts from the next index position of the character a when the last search is found if (index != -1) { //The character iconsole.log(index); //The position of output a}} while (index != -1);</script>After the above code is run, the console outputs
Go to the topic and find the most characters in the string 'zhaochucichuzuiduodezifu'
Method 1: Use an array (there is a disadvantage. When there are more than one character that appears, only one can be found)
<script>var str = "zhaochucichuzuiduodezifu";var arr = [];//Define a new array//Loop through the string for (var i = 0, length = str.length; i < length; i++) {var index = -1;var j = 0;//Find each character do {index = str.indexOf(str[i], index + 1);if (index != -1) {j++;}}while (index != -1);arr[j] = str[i]; // Assign the characters in the string str to the data with the array arr indexed as j. After multiple loops, repeated assignments will occur. //The subsequent assignment will overwrite the previous assignment, but it will not affect our finding the one with the most characters}console.log(arr);console.log("The most characters are" + arr[arr.length - 1]);console.log("The number of times is" + (arr.length - 1));</script>The result of the above code being run and then outputting the console is as follows:
It can also be seen from the output array arr that this method will overwrite characters with the same number of times and can only display one. If 2 characters appear with the same maximum number of times, this method can only obtain one. Based on this, refer to the next method to solve it with objects.
Method 2: Use objects (recommended)
<script>var str = "zhaochucichuzuiduodezifu";var o = {};for (var i = 0, length = str.length; i < length; i++) {// var char = str[i];var char = str.charAt(i);if (o[char]) { //char is an attribute of object o, o[char] is the attribute value, o[char] controls the number of occurrences o[char]++; // Add 1} else {o[char] = 1; //If the first occurrence, the number of occurrences is marked as 1}}console.log(o); //The output is a complete object, recording each character and its occurrence number //Traveling through the object, finding the characters with the most occurrences and the number of occurrences var max = 0;var maxChar = null;for (var key in o) {if (max < o[key]) {max = o[key]; //The maxChar with the largest number of occurrences maxChar = key; //The corresponding character is the current key}}console.log("The most occurrences are" + maxChar);console.log("The number of occurrences is" + max);</script>The result of the above code being run and then outputting the console is as follows:
This method solves the problem of method one, and we can clearly record the number of occurrences of each character. When there are two characters with the same number of times, they can be clearly seen in the object.
However, there are still shortcomings. You cannot directly output the characters with the highest number of times at the same time. This requires additional judgment conditions. The perfect code is as follows O(∩_∩)O
<script>var str = "nininihaoa";var o = {};for (var i = 0, length = str.length; i < length; i++) {var char = str.charAt(i);if (o[char]) {o[char]++; //Add 1} else {o[char] = 1; //If the first occurrence is recorded, the number of times is recorded as 1}}console.log(o); //The output is a complete object, recording each character and its occurrence number//Traveling through the object and finding the character with the most occurrences var max = 0;for (var key in o) {if (max < o[key]) {max = o[key]; //The one with the largest number of max always stored }}for (var key in o) {if (o[key] == max) {//console.log(key);console.log("The most characters are" + key);console.log("The number of occurrences is" + max);}} </script>The results are as follows:
The above is the character that the editor introduces to you that the most common characters appear in the JS string. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!