First, the Loop obj
Let teststr = ' asdasddsfdsfadsfdghdadsdfdgdasd '; functionGetmax (str) {Let obj= {}; for(Let Iinchstr) { if(Obj[str[i]]) {Obj[str[i]]++; }Else{Obj[str[i]]= 1; }} Let Keys= Object.keys (obj);//gets the value of all keys in the object return arrayLet values = object.values (obj);//get all value return arrayLet Maxval = Math.max (... values);//Math.max can find the maximum value of the incoming parameter, such as: Math.max, which can be used to deconstruct in ES6,
can also use Math.max.apply (math,values) can be considered as apply (Math.max, arr)
Arr is then a list of parameters, and for the Max method, its arguments are a number of numbers, i.e. Math.max (a, B, C, D, ...)Console.log (Keys[values.indexof (maxval)],maxval); } getmax (TESTSTR);//obj value: {a:5, s:7, D:12, F:4, G:2, H:1, S:7,}
Keys[values.indexof (maxval)] = The position of the largest number inside the value, and the key that corresponds to the maximum number position.
Maxval the largest number, that is, the number of times.
Str[i] is the first I character in the corresponding string,
Obj[str[i]] is the corresponding letter
The For loop is a key to the object that appears in obj, and then the loop determines if obj has an object with Str[i] as key, and if so, adds one to its value, indicating that the letter appears more often in obj; First I is the key name , Str[i] is the key value, which is used to do the key name of obj, and then assign, or accumulate
eg
Obj[str[i]] = 1; = = var obj={};obj.aa = 1; Console.log (obj)
Second, the regular
Let Stringmax = (str) + = {= Str.split ("). Sort (). Join ('); var s = Str.match (/(\w+) (\1)/g); if NULL ) { return str[0]; } = S.map (E = e=e+e[0]); var out = S.sort ((A, B) =>b.length- a.length); Console.log (out[0][0],out[0].length); Stringmax (TESTSTR)
JS FAQ: Find out the most frequently occurring characters in a string.