Count Characters in JavaScript Case Insensitively



Counting characters in a string is a common task in programming, especially when analyzing text or creating applications that handle user input. Counting characters in a string without worrying about uppercase or lowercase letters is a common task in text processing and data analysis. JavaScript offers different methods to easily count how many times each character appears, regardless of case.

Understanding Case Insensitive

Case insensitivity in programming means that a system takes uppercase and lowercase letters as the same. For example, "Hello" and "hello" would be treated as equal in a case-insensitive situation.

Case-Insensitive Character Counting

You can follow the below mentioned methods to count characters case insensitivity in JavaScript.

Using Loop

To Count Characters Case insensitive using loop, follow the steps mentioned below:

  • Firstly, convert the string to lowercase (or uppercase) to make the comparison case insensitive.
  • Then, iterate through the characters and store their counts in an object.
  • In the last, display the result.

Example Code

Here is the example code for counting case insensitive characters while following the above mentioned steps:

const string = 'ASASSSASAsaasaBBBASvcdNNSASASxxzccxcv';
const countFrequency = str => {
    const frequency = {};
    for(char of str.toLowerCase()){
        if(!frequency[char]){
            frequency[char] = 1;
        }else{
            frequency[char]++;
        };
    };
    return frequency;
};
console.log(countFrequency(string));

Output

{ a: 10, s: 11, b: 3, v: 2, c: 4, d: 1, n: 2, x: 3, z: 1 }

The code defines a string and a function called countFrequency that counts how many times each character appears in that string, ignoring case. It starts by converting the string to lowercase and initializes an empty object to store character counts. As it loops through each character, it checks if the character is already in the object: if not, it adds it with a count of 1; if it is, it increments the count. Finally, the function returns the object with the character counts, and the result is logged to the console, showing the frequency of each character in the original string.

Using Map() Method

To Count Characters Case insensitive using map() method, follow the steps mentioned below:

  • Firstly, convert the string to lowercase (or uppercase) to make the comparison case insensitive.
  • Then, iterate through the characters and store their counts in an object.
  • In the last, display the result.

Example Code

Here is the example code for counting case insensitive characters while following the above mentioned steps:

function countCharactersUsingMap(str) {
    str = str.toLowerCase();
    let charMap = new Map();
    
    for (let char of str) {
        if (char.match(/[a-z0-9]/)) {
            charMap.set(char, (charMap.get(char) || 0) + 1);
        }
    }
    
    return Object.fromEntries(charMap);
}

// Example usage
let str = "JavaScript is Awesome!";
console.log(countCharactersUsingMap(str));

Output

{
j: 1,
a: 3,
v: 1,
s: 3,
c: 1,
r: 1,
i: 2,
p: 1,
t: 1,
w: 1,
e: 2,
o: 1,
m: 1
}

The code defines a function called countCharactersUsingMap that counts how many times each alphanumeric character appears in a string, ignoring case. It converts the string to lowercase and uses a Map to store the counts. The function loops through each character, checking if it's alphanumeric, and updates the count in the Map. Finally, it converts the Map to an object and returns it. In the example, the string "JavaScript is Awesome!" is passed to the function, and the character counts are logged to the console.

Conclusion

Counting characters without considering uppercase or lowercase letters in JavaScript is easy with toLowerCase() and by looping through the characters. Developers can use an object or a Map to quickly count how many times each character appears in a string, which is helpful for analyzing text and processing data.

Updated on: 2025-03-11T16:38:27+05:30

457 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements