The JavaScript implementation computes the most occurrences of the character and the number of occurrences in the string

Source: Internet
Author: User

This article mainly introduces the JavaScript implementation of the number of occurrences of the most frequent characters and the number of occurrences, this article directly gives the implementation code, the need for friends can refer to the

"What is the most frequently occurring character in a string, and how many times has it occurred?" ”

See this demand, I think most people should first think of the conversion of the array, and then do the processing, of course, can solve the problem, and then provide a clever algorithm design, without an array, you can quickly solve the problem, 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);

Related Article

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.