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.