such as SSSFGTDFSSDDFSSSFSSSS, the most frequently seen character is S, which appears 12 times
Traditional wording
Analysis:
1. Prepare an empty JSON, by looking at each character of the loop string, if there's no such character in JSON, create a new array in JSON and put the character in the array, if that character in JSON continues to add the character into the array, then there are n arrays in the JSON loop.
2. Find the longest length of the array in JSON, at this point the length is the number, and the most characters is this parameter, used to for...in ... Loops and Parameters attr
var str= "Sssfgtdfssddfsssfssss";
function Max () {
var json={};
var num=0;
var value=null;
for (Var i=0;i<str.length;i++) {
var k=str[i];
if (!json[k]) {
json[k]=[];
}
Json[k].push (k); Here does not need else, otherwise only the existence of this word Fu Shicai added. Less times
} for
(Var attr in json) {
if (num<json[attr].length) {
num=json[attr].length;
VALUE=JSON[ATTR][0];
}
Alert ("The most common characters are:" +value+ ", the number of occurrences are: ' +num";
};
Max (str);
What do you do if you don't want to put something in JSON?
Analysis:
1. Prepare an empty JSON, looking at each character of the loop string, if the character is not in JSON, set the number of this character to 1, if there is a number + +
2. The character in the loop JSON, as long as it exists, assigns his number to a variable, and each time it compares the new number of characters and the size of the variable, if it is larger than the variable, the value of the variable is updated, and the last variable is the maximum number of characters.
And the most characters are the characters in JSON.
var str= "Sssfgtdfssddfsssfssss";
function Max () {
var json={};
for (Var i=0;i<str.length;i++) {
var k=str[i];//k is all characters, and strings are the same as arrays. Each child element
if (json[k) is fetched by the bracket subscript method {
json[k]++; JSON has this character, put the number of this character +1,
}
else{
json[k]=1;//Otherwise set the number of this character to 1
}
}
var num=0;
var value=null;
For (var k in JSON) {//s, F, G, T, D
if (json[k]>num) {
num=json[k];
Value=k
}
}
Alert ("The most common characters are:" +value+ ", the number of occurrences are: ' +num";
};
The regular method
Analysis:
1. The string is sorted into an array so that the exact same characters are selected.
2. Match the largest number of characters and quantities by using the two parameters of the regular replace () method
var str= "Sssfgtdfssddfsssfssss";
var num=0;
var value=null;
function Max () {
var new_str=str.split (""). Sort (). Join ("");
var re=/(\w) \1+/g; No \1,re is a whole sequence of strings, with \1 is the occurrence of repeated removal, \1 is the same as the first subkey is the same
new_str.replace (Re,function ($0,$1) {//$ 0 represents a whole that is removed to repeat, such as [s,s...],[f,f..],[d,d ...] represents the character
if (num<$0.length) {num=$0.length in this whole
;
Value=$1
}
});
Alert (value+ ":" +num)
};
Max (str);