JavaScript: Calculate the most frequently occurring characters and the number of occurrences of strings _ javascript tips-js tutorial

Source: Internet
Author: User
This article describes how to use JavaScript to calculate the most frequently occurring characters and the number of occurrences of strings. This article provides the implementation code, for more information, see "calculate the maximum number of characters that appear in a string. How many times are there?"

Seeing this demand, 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 we provide a clever algorithm design without the need to convert the array, the Code is as follows:

The Code is as follows:


Var str = "adadfdfseffserfefsefseeffffftsdg ";
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 );

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.