Image credits: Pixelstalks

Question

Write a function to return the maximum occurring character in the input string e.g., if input string is “Test” then function should return ‘t’.
Note: capital letter ‘T’ and small letter ‘t’ should count as the same.

In writing a function to satisfy the above question, the first thing to do is to change every input entered by the user to lower case.
Let us consider the string ‘Test’, our expected result is that the letter ‘t’ should return 2, the letter ‘e’ and ‘s’ should return 1.
To make this happen, we would have to first change all the letters to a lower case like so:

let str = 'Test'
const myStr = str.toLowerCase();

console.log(myStr);
// returns 'test'

The approach we’d use to solving this algorithm question is to return all the characters within the string into an object that displays each character and the number of times they appear, we would then return the letter that has the highest value as our result.
So let’s say I have a string const myString = "Hello World" I can create some kind of loop (forLoop, whileLoop, forEach, forOf, etc) on the string that returns each character in the string into a new object.
In my approach, I’d be using a forOf loop.

const str = "Hello World";
const myStr = str.toLowerCase();
const charMap = {};

for (let char of myStr){ // i.e for every character(char) of myStr
     charMap[char] // adds the new character to the charMap object
}

If you have never worked with the forOf loop before, the snippet above pretty much explains how it works.

const str = "Hello World";
const myStr = str.toLowerCase();
const charMap = {};

for (let char of myStr){
     if (!charMap[char]) {
       charMap[char] = 1;
      } else {
       charMap[char]++;
      }
  }

console.log(charMap)
// result { h: 1, e: 1, l: 3, o: 2, ' ': 1, w: 1, r: 1, d: 1 }

So the loop above return a number of 1 if the character does not exist in the object before else it would increment the number by 1 if the character already exists within the object. If we console.log the results, we should each character with the number of time they appear printed to us in the console.

So now that we have all the characters in the object printed to us alongside with the number of times they appear in the object, we’ll then have to loop through the results again to return the character with the highest number.

We would start off by declaring two helper variables at the top of our program like so:

let max = 0; // set to zero
let maxChar = ''; // set to an empty string

We would iterate through charMap , if we ever have a character that has more uses than max(which is initially zero) then we’ll set maxto be equal to that new number and we’ll set maxCharto be equal to that character that holds the number of max (now you should read that slowly again to get it).

for (let char in charMap) {
  if (charMap[char] > max) {
    max = charMap[char];
    maxChar = char;
    }
  }

Writing out our program and bootstrapping it into a function, our final code should look like this:

To try out your own approach to solving this problem.
- Clone https://github.com/seunzone/js-algorithms
- Install dependencies using yarn install (Assumes you have node installed on your computer).
- Write your code in the index.js file of the maxchar folder.
- In the terminal run yarn run maxchar to see if your code passes the test.

Thanks for reading.