DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Edited on

Algorithm 101: 6 Ways to Find the Most Recurring Character in a String

Can you find the most recurring character in a given string?

Today's episode of Algorithm 101 features 6 Ways to Find the Most Recurring Character in a String

 mostRecurringChar("samson"); // "s" mostRecurringChar("njoku samson ebere"); // "e" 
Enter fullscreen mode Exit fullscreen mode

Prerequisite

This article assumes that you have basic understanding of javascript's string, array and object methods.

Let's Find the Most Recurring Character using:

  • for...in, for...of, hasOwnProperty() and if...statement
 function mostRecurringChar(text) { let charMap = {}; let maxValue = 0; let maxKey = ""; for (char of text) { if (charMap.hasOwnProperty(char)) { charMap[char]++; } else { charMap[char] = 1; } } for (char in charMap) { if (charMap[char] > maxValue) { maxValue = charMap[char]; maxKey = char; } } return maxKey; } 
Enter fullscreen mode Exit fullscreen mode
  • for...in, for...Loop, hasOwnProperty() and if...statement
 function mostRecurringChar(text) { let charMap = {}; let maxValue = 0; let maxKey = ""; for (let i = 0; i <= text.length; i++) { if (charMap.hasOwnProperty(text[i])) { charMap[text[i]]++; } else { charMap[text[i]] = 1; } } for (char in charMap) { if (charMap[char] > maxValue) { maxValue = charMap[char]; maxKey = char; } } return maxKey; } 
Enter fullscreen mode Exit fullscreen mode
  • for...of, if...statement, hasOwnProperty(), Object.keys(), Object.values(), indexOf() and Math.max()
 function mostReoccuringChar(text) { let charMap = {}; let charMapKeys = []; let charMapValues = []; let maxValue = 0; for (char of text) { if (charMap.hasOwnProperty(char)) { charMap[char]++; } else { charMap[char] = 1; } } charMapKeys = Object.keys(charMap); charMapValues = Object.values(charMap); maxValue = Math.max(...charMapValues); return charMapKeys[charMapValues.indexOf(maxValue)]; } 
Enter fullscreen mode Exit fullscreen mode
  • for...Loop, if...statement, hasOwnProperty(char), Object.keys(), Object.values(), indexOf() and Math.max()
 function mostReoccuringChar(text) { let charMap = {}; let charMapKeys = []; let charMapValues = []; let maxValue = 0; for (let i = 0; i <= text.length; i++) { if (charMap.hasOwnProperty(text[i])) { charMap[text[i]]++; } else { charMap[text[i]] = 1; } } charMapKeys = Object.keys(charMap); charMapValues = Object.values(charMap); maxValue = Math.max(...charMapValues); return charMapKeys[charMapValues.indexOf(maxValue)]; } 
Enter fullscreen mode Exit fullscreen mode
  • for...of, if...statement, hasOwnProperty(char), Object.entries
 function mostReoccuringChar(text) { let charMap = {}; let charMapEntries = []; let maxValue = 0; let maxKey = ""; for (char of text) { if (charMap.hasOwnProperty(char)) { charMap[char]++; } else { charMap[char] = 1; } } charMapEntries = Object.entries(charMap); for (entry of charMapEntries) { if (entry[1] > maxValue) { maxValue = entry[1]; maxKey = entry[0]; } } return maxKey; } 
Enter fullscreen mode Exit fullscreen mode
  • for...Loop, if...statement, hasOwnProperty(char), Object.entries
 function mostReoccuringChar(text) { let charMap = {}; let charMapEntries = []; let maxValue = 0; let maxKey = ""; for (let i = 0; i <= text.length; i++) { if (charMap.hasOwnProperty(text[i])) { charMap[text[i]]++; } else { charMap[text[i]] = 1; } } charMapEntries = Object.entries(charMap); for (entry of charMapEntries) { if (entry[1] > maxValue) { maxValue = entry[1]; maxKey = entry[0]; } } return maxKey; } 
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. You are only limited by your imagination. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

Up Next: Algorithm 101: 8 Ways to Capitalize a Sentence

You can also follow and message me on social media platforms.

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (0)