Open In App

JavaScript Program to Count the Occurrences of a Specific Character in a String

Last Updated : 09 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to count the frequency of a specific character in a string with JavaScript. Counting the frequency of a specific character in a string is a common task in JavaScript.

Example:

Input : S = “geeksforgeeks” and c = ‘e’
Output : 4
Explanation: ‘e’ appears four times in str.

Input : S = “abccdefgaa” and c = ‘a’
Output : 3
Explanation: ‘a’ appears three times in str.

We will explore every approach to counting the occurrences of a specific character in a string, along with understanding their basic implementations.

Using for loop

This approach involves iterating through the string character by character using a for loop. It initializes a counter to keep track of the count of the target character.

Syntax:

for (let i = 0; i < str.length; i++) {
if (str[i] === targetChar) {
count++;
}
}

Example : This example shows the implementation of the above approach.

Javascript




function countFrequency(
    inputString,
    targetChar
) {
    let count = 0;
    for (
        let i = 0;
        i < inputString.length;
        i++
    ) {
        if (
            inputString[i] ===
            targetChar
        ) {
            count++;
        }
    }
    return count;
}
  
const text = "Hello Geeks!";
const charToCount = "l";
  
console.log(
    countFrequency(text, charToCount));


Output

2

Using split() Method

The string is first split into an array of individual characters using the split() method. It then utilizes the filter() method to create a new array containing only the target character.

Syntax:

const charArray = str.split('');

Example : This example shows the implementation of the above approach.

Javascript




function countFrequency(
    inputString,
    targetChar
) {
    const stringArray =
        inputString.split("");
    const count = stringArray.filter(
        (char) => char === targetChar
    ).length;
    return count;
}
  
const text = "Hello Geeks!";
const charToCount = "e";
  
console.log(
    countFrequency(text, charToCount));


Output

3

Using match() Method

It constructs a regular expression that matches the target character globally (‘g’ flag) in the string. The match() method returns an array of all matches found.

Syntax:

const regex = new RegExp(targetChar,"g");
const matches = str.match(regex);

Example : This example shows the implementation of the above approach.

Javascript




function countFrequency(
    inputString,
    targetChar
) {
    const regexPattern = new RegExp(
        targetChar,
        "g"
    );
    const frequencyMatches =
        inputString.match(regexPattern);
    const counter = frequencyMatches
        ? frequencyMatches.length
        : 0;
    return counter;
}
  
const text = "Hello Geeks!";
const charToCount = "H";
  
console.log(
    countFrequency(text, charToCount));


Output

1


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads