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"
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;
}
- 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;
}
- 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)];
}
- 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)];
}
- 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;
}
- 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;
}
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.
You can also follow and message me on social media platforms.
Thank You For Your Time.
Top comments (0)