If you want to keep your technology vibrant, the most effective way is to provide sufficient nutrients through continuous input. We don't have to deliberately pursue advanced or new knowledge points. We can also gain a lot through a comprehensive and multi-dimensional analysis of a basic problem. topicSuppose there is such a question: Please get the character with the most repetitions and its repetition times in the string "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,suzaoyouqudelinghun." Let’s solve this problem today. analyzeThe solution to this type of problem is relatively open, and the implementation methods may be varied. The difference lies in the high or low running performance of the code (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. Objects of useSolution:
The code is implemented as follows:const testStr = "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,suzaoyouqudelinghun.ii"; // Get the mapping object of each character and its repetition number let wordsObj = {}; for (let index = 0; index < testStr.length; index++) { const word = testStr[index]; word in wordsObj ? wordsObj[word]++ : wordsObj[word] = 1; } // Get the maximum number of repetitions let maxNum = 0; for (const word in wordsObj) { const num = wordsObj[word]; if (num >= maxNum) { maxNum = num; } } // Get the character corresponding to the maximum number of repetitions and output the result for (const word in wordsObj) { const num = wordsObj[word]; if (num === maxNum) { console.log(`The character with the most repetitions is: ${ word }, the number of repetitions is: ${ maxNum }`) } } // The character that appears most often is: i, and the number of times it appears is: 10 // The character that appears most often is: u, and the number of times it appears is: 10 analyze:
Arrays & PointersSolution:
The code is implemented as follows:const testStr = "bianchengsanmei,xuexiyouqudezhishi,jieshiyouqudepengyou,suzaoyouqudelinghun.ii"; // Convert the string to an array and sort it const testStrArr = testStr.split("").sort(); let startIndex = 0; let endIndex = 1; let maxNum = 0; let validWords = []; //Use pointer method to obtain the maximum number of repetitions and the character array corresponding to the maximum number while (startIndex < testStrArr.length) { // The characters at 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 storing the qualified characters validWords = [testStrArr[startIndex]]; } else if (rangeNum === maxNum) { // If the new number is equal to the maximum number, push the character into the character array validWords.push(testStrArr[startIndex]); } startIndex = endIndex; } endIndex++; } // Print results for (let index = 0; index < validWords.length; index++) { const word = validWords[index]; console.log(`The most repeated is: ${ word }, the number of repetitions is: ${ maxNum }`) } analyze:The difficulty and essence of this method lies in the use of pointer method, which allows us to obtain the desired result in one cycle. SummarizeThese are probably the two mainstream ideas for solving problems, and many other solutions can be seen as variations of these two ideas. No matter how things change, the essence remains the same. As long as you have a clear idea of how to solve the problem, code implementation is just a result. In our daily work and study, we must consciously cultivate our divergent thinking and look at problems from multiple angles. You may discover different scenery! This is the end of this article on how to use JavaScript to get the most repeated characters in a string. For more relevant JS content on getting the most repeated characters, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Beginners understand MySQL deadlock problem from source code
>>: Understanding MySQL deadlock routines through unique index S lock and X lock
Table of contents 1. Array flattening (also known...
Environment Preparation 1. Environment Constructi...
introduction Xiao A was writing code, and DBA Xia...
Table of contents 1. Basic Examples 2. Computed p...
Effect Need environment vue elementUI Drag and dr...
Today, I want to write about a "low-tech&quo...
1. Phenomenon In the early morning, an index was ...
Arrow function is a new feature in ES6. It does n...
1. Basic Use It can be instantiated through the M...
Introduction MySQL achieves high availability of ...
union execution For ease of analysis, use the fol...
Today we will talk about how to use Jenkins+power...
1. Use frameset, frame and iframe to realize mult...
1. Problem There is a table as shown below, we ne...
What is CN2 line? CN2 stands for China Telecom Ne...