Open In App

Maximum Frequency Character in String in JavaScript

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A string is a data structure in JavaScript that is used to store a set of characters. A string can contain a character, a word, or a sentence. A string also can be empty. It is represented using double quotes, single quotes, or template literals.

Examples:

Input: str = "abcdabac"
Output: a
Explanation: In the given string the number of occurrence of character is a is3, b is 2, c is 2, and d is 1.
Therefore 'a' is the character which has maximum frequency.
Input: str = "bZzDbc"
Output: b
Explanation: In the given string the number of occurrence of character is b is 2, Z is 1, z is 1, D is 1, d is 1, c is 1.
Therefore 'a' is the character which has maximum frequency.

We can solve this problem using the methods which are listed below:

Brute force method

The idea in this method is to use simple nested loops. By looping through the string starting from first character we will measure the frequency of the character throughout the string. In the next iteration of outer loop we will start from the next character and so on. While doing this we will keep track of the character of maximum frequency till now and also it’s frequency. If the current character will have more frequency then we will update both of them.

Example: The below code uses the nested loops to find the maximum frequency character in a string.

Javascript




function findMaxFrequencyChar(inputString) {
    let maxFreq = 0;
    let maxFreqChar = '';
    for (let i = 0; i < inputString.length; i++) {
        let currChar = inputString[i];
        let currFreq = 0;
        for (let j = 0; j < inputString.length; j++) {
            if (inputString[j] === currChar) {
                currFreq++;
            }
        }
        if (currFreq > maxFreq) {
            maxFreq = currFreq;
            maxFreqChar = currChar;
        }
    }
    return maxFreqChar;
}
 
const str1 = "abcdabac";
const str2 = "bZzDbc";
const maxFreChar1 = findMaxFrequencyChar(str1);
const maxFreChar2 = findMaxFrequencyChar(str2);
console.log
    (`The maximum frequency character in string ${str1} is ${maxFreChar1}`);
console.log
    (`The maximum frequency character in string ${str2} is ${maxFreChar2}`); 


Output

The maximum frequency character in string abcdabac is a
The maximum frequency character in string bZzDbc is b

Time complexity: O(n2), where n is the length of the string.

Space complexity: O(1)

Using JavaScript Object

In this method, we will iterate through the array values and store each one of them in an JavaScript object as a key and their count as their values, then we iterate through the object to check the maimum frequency character.

Example: The below code will explain how to implement above-discussed approach in JavaScript.

Javascript




function findMaxFrequencyChar(inputString) {
    let charFrequency = {};
    for (let char of inputString) {
        if (charFrequency[char]) {
            charFrequency[char]++;
        } else {
            charFrequency[char] = 1;
        }
    }
    let maxFreq = 0;
    let maxFreqChar = '';
 
    for (let char in charFrequency) {
        if (charFrequency[char] > maxFreq) {
            maxFreq = charFrequency[char];
            maxFreqChar = char;
        }
    }
    return maxFreqChar;
}
 
const str1 = "abcdabac";
const str2 = "bZzDbc";
const maxFreChar1 = findMaxFrequencyChar(str1);
const maxFreChar2 = findMaxFrequencyChar(str2);
console.log
    (`The maximum frequency character in string ${str1} is ${maxFreChar1}`);
console.log
    (`The maximum frequency character in string ${str2} is ${maxFreChar2}`);


Output

The maximum frequency character in string abcdabac is a
The maximum frequency character in string bZzDbc is b

Time complexity: O(N+M), where N is the length of given string and M is the number of unique characters in string.

Space complexity: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads