?? JavaScript Interview Challenge - Day 2??


Challenge: Can you create a JavaScript function to find the character that repeats the most in a given string?

let str = "hello world";

So in this string you have to return i. it's repeating 3 times.

Instructions:

  • If there are multiple characters with the same maximum frequency, return the first one encountered.
  • Ensure your function handles empty strings gracefully by returning null.


?? Challenge Yourself! ??

Take on this captivating JavaScript interview problem and see if you can solve it without peeking at the solution!


Solution :

function findMaxRepeatingChar(str) {
    if (str.length === 0) {
        return null; // Return null for an empty string
    }

    let charMap = {}; // Object to store character frequencies

    // Loop through the string to count character frequencies
    for (let char of str) {
        charMap[char] = (charMap[char] || 0) + 1;
    }

    let maxChar = '';
    let maxCount = 0;

    // Loop through the character map to find the character with the maximum frequency
    for (let char in charMap) {
        if (charMap[char] > maxCount) {
            maxChar = char;
            maxCount = charMap[char];
        }
    }

    return maxChar;
}

// Example usage:
const exampleString = "hello world";
const maxRepeatingChar = findMaxRepeatingChar(exampleString);
console.log("Maximum repeating character:", maxRepeatingChar); // Output: 'l'
        

  • Input Validation: The function first checks if the input string is empty. If it is, the function returns null, indicating that there are no characters to analyze.
  • Character Frequency Counting: The function creates an empty object (charMap) to store the frequencies of each character in the string. It then loops through each character in the input string, incrementing its count in the charMap object.
  • Finding the Maximum Repeating Character: After creating the frequency map, the function initializes variables maxChar and maxCount to keep track of the character with the highest frequency and its count, respectively. It then iterates through the charMap object and compares the count of each character with the current maximum count. If the count of the current character is greater than the current maximum count, it updates maxCount and sets maxChar to the character.
  • Returning the Result: Finally, the function returns the character (maxChar) with the highest frequency. If there are multiple characters with the same maximum frequency, it returns the first one encountered in the string.

This approach efficiently counts the occurrences of each character using an object (hash map) and then finds the character with the highest frequency through iteration.

Anil Kumar

Angular developer

1 年

let abc = "hello world"; let maxChar; let maxCount = 0; for (i = 0; i < abc.length; i++) { let count = 0; for (j = 0; j < i + 1; j++) { if (abc[i] === abc[j]) { count++; } } if (count > maxCount) { maxCount = count; maxChar = abc[i]; } } console.log(maxChar, maxCount)

回复
Rineez Ahmed N

CTO at Livares Technologies Pvt. Ltd. | Software Architect | Quantum Computing & Machine Learning Enthusiast

1 年

*Simple Approach* Initialize maxChar='' and maxCount=0. Iterate through string each char. Take count of each char and store as key-value pairs. When a count is higher than maxCount or maxChar is empty, assign count to maxCount and char to maxChar. After the loop print maxChar. Time complexity O(n) Space complexity O(n)

Harish Kumar

Senior Software Engineer @ Uneecops Technologies Ltd. | MCA

1 年

Thank for sharing Sanjeev Kumar

要查看或添加评论,请登录

Sanjeev Kumar的更多文章

社区洞察

其他会员也浏览了