In a string, such as ' Zhaochucichuzuiduodezifu ', we want to find the characters that appear most. This article will explain the method of thinking in detail.
First, we introduce two methods in two string objects : IndexOf () and Charat () methods
IndexOf () method describes returning a specified string value for the first occurrence in a string
CharAt () method describes returning a character at a specified location
Start with a little test to find out where every ' I ' in the string ' Woainixiaoli ' appears.
<script>
var arr = ' Woainixiaoli ';
var index =-1; Define variables Index control index value
//When lookup does not reach a, that is, the value of IndexOf () is-1, the end loop do
{
index = arr.indexof ("i", index + 1);//Use the second parameter index+ 1, control every lookup is from the last to find the next index position of character A to start
if (index!=-1) {//can find the character I
console.log (index);//output A's position
}
} (Index!=-1);
</script>
After the above code is run, the console output is
To get to the point, ask for the most characters of the string ' Zhaochucichuzuiduodezifu '
Method One: Use an array (there is a disadvantage, when most characters appear more than one, can only find one)
<script>
var str = "Zhaochucichuzuiduodezifu";
var arr = [];//defines a new array
//loop Traversal 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]; The character in string str is assignments to the data of the array arr index J, and after multiple loops there will be repeated assignment, and/
/After the assignment will overwrite the previous assignment, but it does not affect the one that we find the most characters.
con Sole.log (arr);
Console.log ("The most characters are" + arr[arr.length-1]);
Console.log ("number is" + (arr.length-1));
</script>
After the above code runs, the results of the console output are shown below:
From the output of the array arr can also be seen, this method will be the same number of characters overwritten, can only display one. If 2 characters appear the same maximum number of times, this method can only draw one. Based on this, refer to the next method that is resolved with the object.
Method Two: Using the object (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 a property value, O[char] control the number of occurrences
o[char]++;//times plus 1
} else {
O[char] = 1;///If the first time appears , the number of times is 1
}
}
console.log (o);//output is a complete object, recording each character and the number of occurrences
//Traversing the object, find the most occurrences of the characters and times
var max = 0;
var maxchar = null;
for (var key in O) {
if (Max < O[key]) {
max = O[key];//max always store the largest number of
Maxchar = key;//Then the corresponding character is the current key
}
}
Console.log ("The most characters are" + Maxchar);
Console.log ("The number of occurrences is" + max);
</script>
After the above code runs, the results of the console output are shown below:
This method solves the problem of method one, and each character we can clearly record the number of occurrences, when there are two times the same characters, you can see clearly in the object.
However, there is still insufficient to directly output the highest number of characters at the same time, which requires additional judgment conditions. The perfect code is as follows Ha 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]++;//times plus 1
} else {
O[char] = 1;////////////////If first occurrence, 1
}
}
console.log (o);// The output is a complete object that records each character and the number of times it appears
//Traverses the object, finds the number of characters that appear most frequently
var max = 0;
for (var key in O) {
if (Max < O[key]) {
max = O[key];//max always store the maximum number of
}
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 a small set of JS to find out the most occurrences of the characters in the string, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!