Javascript Algorithms: Find The Most Recurring Value

JavaScript Algorithms: Find the Most Recurring Value

As I prepare for Javascript development interviews I've been reviewing various online articles and resources about different algorithms I'll likely get asked about. Turns out that these things are actually pretty fun exercises and while I was able to do some of them all by my lonesome, others required some documentation lookups and Googling. Like any development problem there are likely multiple good solutions but the ones I present here are probably the most direct and obvious - feel free to solve differently and generate your own solution.

Where possible I'll cite my source(s) and give credit to any resources that helped me solve this algorithm.

FIND THE MOST RECURRING VALUE

Resources:

https://scotch.io/courses/the-ultimate-guide-to-javascript-algorithms/finding-the-most-recurring-character

Write a solution in Javascript where, if I give you a string of letters you'll return the one character that occurs the most in that string. For example, if I provide "abca" your solution will return "a".

USAGE: Copy and paste the code samples below into the Javascript console in your browser, then simply hit return on your keyboard to see the code in action.

//FIND THE MOST RECURRING CHARACTER IN A STRING
//character mapping comes in handy here.... 
function maxRecurringChar(text) {
    let charMap = {}  //create an empty object to hold each char's value and number of occurrences
    let maxCharValue = 0 //used when it's time to find each char's number of occurrences
    let maxChar = '' //will hold the actual char used the most in the submitted string

    for (let char of text) {  //loop over each character in the submitted text
        if (charMap.hasOwnProperty(char)) {  //if this character is already in the charMap then add 1....
            charMap[char]++
        } else {  //...otherwise create it in the charMap and set it's value to 1
            charMap[char] = 1
        }
    }

//now loop over the charMap and tabulate recurrence of each character
    for (let char in charMap) {
        if (charMap[char] > maxCharValue) { //if the current char occurs more than the maxCharValue....
            maxCharValue = charMap[char]  //then set the maxCharValue equal to the char being evaluated
            maxChar = char  //and store the actual char in here
        }
    }

    return maxChar
}

console.log(maxRecurringChar('text'));


Link your website to this page! Copy and paste the URL below:
http://www.cfsnap.com/javascript/javascript-algorithms-find-the-most-recurring-value/
Copyright 2019. All Rights Reserved.