"Calculate what is the character that appears the most frequently in a string, and how many times does it appear?"
Seeing this requirement, I think most people should first think of converting it into an array and then processing it. Of course, it can solve the problem. Then here provides a clever algorithm design, which can solve the problem quickly without turning the array. The code is as follows:
The code copy is as follows:
var str = "adadfdfsefserfefsefseeffffftsdg";
var maxLength = 0;
var result = "";
while(str!=''){
oldStr = str;
getStr = str.charAt(0);
str = str.replace(new RegExp(getStr,"g"),"");
if( oldStr.length-str.length > maxLength){
maxLength = oldStr.length-str.length;
result = getStr + "=" + maxLength;
}
}
alert(result);