JS FAQ: Find out the most frequently occurring characters in a string.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.