Max Character in JavaScript algorithm

Isaac Avilez
3 min readOct 21, 2020

In this blog we’re going to be talking about the maxChar algorithm in JavaScript. Basically this algorithm is when given a string, return the character that is most frequently used in the string. So let’s get started.

We want to start off by obviously writing out our function declaration and giving our function ONE parameter named “(str)”, or whatever you desire. After we’ve written out or function, we want to create a variable with an empty object and give that variable the name of “obj”. The reason we want to do that is so later we can store the key value pairs of the string into the object. Our code should look like so:

Function with empty object.

With that being done, we want to start a for loop. We want to be able to iterate through each character of the string using that for loop. Within our for loop, we want to write out a conditional statement. In our if statement, we want to check if the obj[char] exists, then we increment the value of that key. If obj[char] DOESN’T exist, our else statement will set the value of char to 1. After we’ve written out our conditional statement within the for loop, our code should look similar to this:

Iterating through given string and each of its characters.

Not so quick, we’re nearly there, but we’re not done. After our for loop, we want to declare a variable with the name maxNum and assign it the value of 0. We want that to be 0, because later on we will be able to figure out which key has the highest value through that variable.

With our maxNum being declared and initialised, we want to declare another variable and name it bigChar, and give it the value of an empty string, AKA “ ”. We want to have a big char set to an empty string so that we can return the letter that appears the most frequently within the given string. So far the code should look like this:

Two newly declared and initialised variables.

Nearly there, don’t worry. After our two newly created variables, we would then like to create yet again, another for loop. In this for loop, we would let to iterate over our obj variable. Remember why I said we’d need it later on? We want to iterate over all of over key/value pairs. Guess what? We want to create a conditional within our for loop once again! In our if statement, we check if our obj[char] is greater than our maxNum variable “(obj[char] > maxNum)”, we would like to set our maxNum to our obj[char], and also set our bigChar to whatever character it’s iterating over that appears the most frequently. Make sure to return our bigChar after the for loop. After setting up our conditional within our newly created for loop, and returning bigChar after the loop, our code should look exactly, or similar to this:

Completed maxChar algorithm.

Here we have our newly completed maxChar algorithm that returns the character that appears the most frequently within the given string. I hope you all enjoyed this blog, and more to come soon!

--

--