Javascript gets the most repeated characters and the number of javascript times

Source: Internet
Author: User

Javascript gets the most repeated characters and the number of javascript times

Javascript obtains the most repeated characters

/** Extract the characters with the most repeated words in the string */var words = 'sdfghjkfastgbyhnvdstyaujskgfdfhlaa'; // create the string var word, // length of a single character; // The length of this character // defines the output object var max = {wordName: '', // wordLength: 0 // repeated times }; // recursive method, passed in the string (function (words) {if (! Words) return; // If the string has become null, return, end recursion word = words [0]; // retrieve the first character in the string length = words. length; // set the length to the current String length of words = words. replace (new RegExp (word, 'G'), ''); // returns the remaining String length = length-words that removes the current character. length; // reset the length to the length of the current character in the string if (length> max. wordLength) // if the number of repetitions of this character is greater than maxLength, set maxLength to the number of repetitions of the current character max ={// reset the value of the object wordName: word, wordLength: length }; arguments. callee (words); // recursive call, pass in the remaining string}) (words); console. log (max. wordName + "\ n" + max. wordLength); // output after Recursion

This morning, I accidentally saw this problem. I saw that most of the Internet was made out of two loops. Then I wrote it recursively.

The idea is

Each recursion. Extract the first character. Remove the characters with the same symbol from the string, and subtract the length of the removed string from the previous string.

The number of times the current character in the string is repeated.

Determines whether the number of repeated characters is greater than the maxLength stored in the current output object.

If true, update

Then go to the next recursion until the string is replaced and terminated.

The output object stores the most frequently used characters and repeated times.

The above is all the content of this article. I hope you will like it.

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.