JavaScript gets the most repetitive characters _javascript tips

Source: Internet
Author: User

JavaScript Gets the most repeated characters

/** remove the characters with the most repetition words in the string * * var words = ' Sdfghjkfastgbyhnvdstyaujskgfdfhlaa ';                          Create string var word,//single character length; The length of the character//defines the output object var max = {wordname: ',///Repeat the most number of characters wordlength:0//
Number of times};         Recursive method, passed-in string (function (words) {if (!words) return;         Returns if the string has been empty, ending recursive word = words[0];         Remove the first character in the string length = Words.length; Sets length to the current string length words = Words.replace (new RegExp (Word, ' g '), ');      Returns the remaining string length = Length-words.length that excludes the string from the current character;               Resets the length to the current character in the string in the amount of if (Length > Max.wordlength)//If the number of repetitions is greater than maxlength, reset the maxlength to the current character repeat number max = {              
  Resets the value of the object Wordname:word, wordlength:length};        Arguments.callee (words);
Recursive invocation, passing in the remaining string}) (words);     Console.log (max.wordname+ "\ n" +max.wordlength); Output 
after recursion ends

This morning I stumbled upon a question like this. See the internet is mostly done with two loops. And then I wrote it recursively.

Idea is

Once per recursion. Remove the first character. Removes the same symbol from the string and subtracts the length of the string before it is deleted.

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

Determines whether the number of repetitions of the character is greater than the maxlength stored in the current output object.

If true, the update

Then enter the next recursion until the string is replaced and terminated

The highest number of characters are stored in the output object and the number of repetitions

The above mentioned is the entire content of this article, I hope you can enjoy.

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.