Ask canal that have so clear, to have a source of living water.

The most effective way to keep your technology alive is to feed it with inputs. We do not have to deliberately pursue advanced or fresh knowledge points, through a fundamental problem of a full range of multi-dimensional analysis, will also be a lot of harvest.

The title

Suppose there is such a topic: please get string “bianchengsanmei, xuexiyouqudezhishi, jieshiyouqudepengyou suzaoyouqudelinghun.” in the character repeat the most times and repetitions.

Today we’re going to solve this problem.

Analysis of the

The solution of this kind of problem is relatively open, and the implementation means may be varied, the difference lies in the running performance of the code is high or low (time complexity and space complexity are different).

The only thing to note here is that there may be more than one character that matches the maximum number.

Using the object

To solve the problem:

  • Iterates through the string, taking each character as the key, repeating the number of values, and storing an object.
  • Iterate through the object to get the maximum value.
  • Traverse the object and get the corresponding character key according to the maximum value obtained.
  • Output the results.

The code is implemented as follows:

const testStr = "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,suzaoyouqudelinghun.ii"; Let wordsObj = {}; for (let index = 0; index < testStr.length; index++) { const word = testStr[index]; word in wordsObj ? wordsObj[word]++ : wordsObj[word] = 1; } let maxNum = 0; for (const word in wordsObj) { const num = wordsObj[word]; if (num >= maxNum) { maxNum = num; }} for (const word in wordsObj) {const num = wordsObj[word]; If (num === maxNum) {console.log(' The most repeated character is: ${word}, the most repeated character is: ${maxNum} ')}} // The most frequent character is: I, the most frequent character is: 10 // The most frequent character is: U, the frequency of occurrence is: 10

Analysis:

  • This should be the first solution that many people can think of, in line with the “process oriented” programming idea.
  • There are three cycles in total, and there is a lot of room for optimization.

Arrays & Pointers

To solve the problem:

  • Converts a string to an array and sorts it so that duplicate characters are grouped together.
  • Using the pointer idea, get the maximum number of repetitions and the corresponding character array.
  • Output the results.

The code is implemented as follows:

const testStr = "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,suzaoyouqudelinghun.ii"; // Convert a string to an array and sort it const testStrarr = testStr.split("").sort(); let startIndex = 0; let endIndex = 1; let maxNum = 0; let validWords = []; // Using the pointer method, While (startIndex < testStrarr. Length) {startIndex < testStrarr. Length) {startIndex < testStrarr (testStrArr[startIndex] ! Const rangeNum = endIndex-startIndex; const rangeNum = endIndex-startIndex; if (rangeNum > maxNum) { maxNum = rangeNum; ValidWords = [testStrarr [startIndex]]; // ValidWords = [startIndex]; } else if (rangeNum === maxNum) {// Push the character into the character array validWords. Push (testStrarr [startIndex]); } startIndex = endIndex; } endIndex++; } // Print the result for (let index = 0; index < validWords.length; index++) { const word = validWords[index]; Console. log(' The number of repeats is: ${word}, number of repeats: ${maxNum} ')}

Analysis:

The difficulty and essence of this method is that we use the pointer method and get the desired result in a single loop.

conclusion

There are only two mainstream solutions, and many other solutions can be seen as variants of these two approaches.

All change from its philosophy, as long as the idea of solving the problem is clear, code implementation is just a result. In ordinary job study, we should cultivate his divergent thinking consciously, look at a problem from many angles, you may discover different scenery!

I hope to be able to inspire you!

~

~ This article is over, thanks for reading!

~

Learn interesting knowledge, meet interesting friends, build interesting soul!

I am the author of “programming samadhi” hermit king, my public account is “programming samadhi”, welcome to pay attention to, I hope you give me more advice!

You come, with expectations, I have ink to greet! You return, regardless of gain and loss, only with aftertaste gift!

Pay equal attention to knowledge and skills, internal force and external work and repair, theory and practice both hands should grasp, both hands should be hard!