For example, sssfgtdfssddfssssssss, the character that appears the most is s, which appears 12 times
Traditional writing
analyze:
1. Prepare an empty json and look at each character in the string. If there is no such character in json, create a new array in json and put this character into the array. If there is this character in json, continue to add the characters into the array. At this time, after the loop is completed, there are n arrays in json.
2. Find the longest array in json. At this time, the length is the number, and the most characters are this parameter, using the for...in...loop and parameter attr
var str="sssfgtdfssddfssssss";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); //Else is not needed here, otherwise it will be added only when this character exists. The number of times will be reduced}for(var attr in json){if(num<json[attr].length){num=json[attr].length;value=json[attr][0];}}alert("The character that appears most is: "+value+', and the number of times is: '+num);};max(str);What if you don't want to put things into JSON?
analyze:
1. Prepare an empty json, and look at each character in the string. If there is no such character in the json, set the number of this character to 1. If there is, the number ++
2. Loop the characters in json, as long as they exist, they will assign their number to a variable, and compare the number of new characters with the size of this variable every time. If they are larger than the variable, the value of the variable is updated. Finally, the value of this variable is the number of the most characters.
The most common character is this character in json
var str="sssfgtdfssddfssssss";function max(){var json={};for(var i=0;i<str.length;i++){var k=str[i]; //k is all characters, and strings can also be retrieved by the bracket subscript method like arrays if(json[k]){json[k]++; //When there is this character in json, the number of this character is +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, dif(json[k]>num){num=json[k];value=k;}}alert("The character that appears the most is: "+value+', and the number of occurrences is: '+num);};max(str);Regular method
analyze:
1. Convert the string into an array for sorting, so that the same characters next to each other are selected regularly.
2. Use the two parameters of the regular replace() method to match the most characters and the number of characters
var str="sssfgtdfssddfsssssss";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 sorted string. With /1, it is taken out with repeated occurrences. /1 means that the first child before is the same new_str.replace(re,function($0,$1){ //$0 means that the whole is taken out and repeated, such as [s,s...],[f,f..],[d,d....] $1 represents the character in this whole if(num<$0.length){num=$0.length;value=$1}});alert(value+":"+num)};max(str);