The most effective way to maintain your technological vitality is to provide enough nutrients through continuous input. We don’t have to deliberately pursue advanced or fresh knowledge points. We will also gain a lot through all-round and multi-dimensional analysis of a basic problem.
topic
Suppose there is a question: Please get the character with the most repeated times and the number of repetitions in the string "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,suzaoyouqudelinghun."
Today we will solve this question.
analyze
The solution to this problem is relatively open, and the implementation methods may be diverse. The difference is that the code's running performance is high and low (different time complexity and space complexity).
There is only one thing to note here: there may be more than one character that meets the maximum number of times.
Use object
Problem solution:
- Iterate over the string, use each character as key, and the number of repetitions as value, and store an object.
- Iterate through the object and get the maximum value of value.
- Iterate through the object and obtain the corresponding character key based on the maximum value obtained.
- Output result.
The code implementation is as follows:
const testStr = "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,"; // Get the mapping object for each character and its number of repetitionslet wordsObj = {}; for (let index = 0; index < ; index++) { const word = testStr[index]; word in wordsObj ? wordsObj[word]++ : wordsObj[word] = 1; } // Get the maximum number of repetitionslet maxNum = 0; for (const word in wordsObj) { const num = wordsObj[word]; if (num >= maxNum) { maxNum = num; } } // Get the characters corresponding to the maximum number of repetitions and output the resultfor (const word in wordsObj) { const num = wordsObj[word]; if (num === maxNum) { (`The character that repeats the most is:${ word },The number of repetitions is:${ maxNum }`) } } // The character with the most occurrences is: i, and the occurrences are: 10// The character with the most occurrences is: u, and the occurrences are: 10
analyze:
- This should be the solution that many people can think of at the first time, and it is very consistent with the "process-oriented" programming idea.
- There are three cycles in total, and there is a large room for optimization.
Arrays & Pointers
Problem solution:
- Convert the string into an array and sort it so that the duplicate characters are arranged together.
- Use pointer idea to get the maximum number of repetitions and the corresponding character array.
- Output result.
The code implementation is as follows:
const testStr = "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,"; // Convert strings to arrays and sortconst testStrArr = ("").sort(); let startIndex = 0; let endIndex = 1; let maxNum = 0; let validWords = []; // Use pointer method to get the character array corresponding to the maximum number of repetitions and the maximum number of timeswhile (startIndex < ) { //The characters at the positions of startIndex and endIndex are different if (testStrArr[startIndex] !== testStrArr[endIndex]) { // Calculate the number of characters between startIndex and endIndex const rangeNum = endIndex - startIndex; if (rangeNum > maxNum) { maxNum = rangeNum; // If a new maximum number of times appears, reassign the array that stores the qualified characters validWords = [testStrArr[startIndex]]; } else if (rangeNum === maxNum) { // If the new number and maximum number of times are equal, push the character into the character array (testStrArr[startIndex]); } startIndex = endIndex; } endIndex++; } // Print the resultfor (let index = 0; index < ; index++) { const word = validWords[index]; (`The most repeated:${ word },The number of repetitions is:${ maxNum }`) }
analyze:
The difficulty and essence of this method is that the pointer method is used, and we can get the results we want in one cycle.
Summarize
There are probably two mainstream solutions to solve problems, and many other solutions can be regarded as variants of these two ideas.
All changes will never be separated from the essence. As long as you clarify the problem-solving ideas, the code implementation is just a result. In our daily work and study, we must consciously cultivate our divergent thinking and look at the problem from multiple angles. You may find different scenery!
This is the article about how to use JavaScript to obtain the most repeated characters in a string. For more related JS to obtain the most repeated characters, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!