Skip to content

Instantly share code, notes, and snippets.

@jordanhudgens
Last active September 15, 2018 18:45
Show Gist options
  • Save jordanhudgens/0e135c1d6163ddf5ed0521243cc3db22 to your computer and use it in GitHub Desktop.
Save jordanhudgens/0e135c1d6163ddf5ed0521243cc3db22 to your computer and use it in GitHub Desktop.
const maxStringCounter = str => {
const obj = {};
const splitStr = str.split("").filter(el => el !== " ");
splitStr.forEach(el => {
if (obj[el]) {
return (obj[el] += 1);
} else {
return (obj[el] = 1);
}
});
const values = Object.values(obj);
const keys = Object.keys(obj);
const maxValue = Math.max(...values);
const indexOfLargestValue = values.indexOf(maxValue);
return keys[indexOfLargestValue];
};
maxStringCounter("44123"); // 4
maxStringCounter("441111123"); // 1
maxStringCounter("Some brown and blue fox"); // o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment