Skip to content
Related Articles

Related Articles

How to get the number of occurrences of each letter in specified string in JavaScript ?

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 23 Jun, 2022
Improve Article
Save Article

Given a string, our task is finding the occurrence of a character in the string with the help of user-defined function. 

Example:
Input : "hello"
Output : h occur 1 times
         e occur 1 times
         l occur 2 times
         o occur 1 times
Explanation : here "hello" have 1 h, so it have 1 value.
               as same e have 1, l have 2 , o have 1.
Example 2:
Input : "did"
Output: d occur 2 times 
        i occur 1 times

Approach 1: In this approach we use a map data structure to store the number of times characters occur.

  • First we initialize map with key each character of string and value for each is 0.
  • We iterate over string and increment value of the character.
  • Finally, print key-values of the map.

Example: 

Javascript




<script>
 
//function to print occurrence of character
function printans( ans )
{
  for( let [ key ,value] of ans)
  {
    // if()
    console.log(`${key}  occurs  ${value} times` );
       
  }
 
}
 
// function count occurrence of character
function count( str , outp_map )
{
  for( let i = 0 ;i < str.length ;i++)
  {
 
    let k = outp_map.get(str[i]);
    outp_map.set(str[i], k+1) ;
         
     
  }
  //calling  print function
  printans(outp_map);
}
 
//function create map to count character
function count_occurs( test , callback )
{
  //checking string is valid or not
  if( test.length === 0 )
  {
    console.log(" empty string ");
    return ;
  }
  else
  {
    // map for storing count values
    let ans = new Map();
    for( let i = 0 ;i < test.length;i++)
    {
      ans.set(test[i], 0);
    }
     
    callback( test ,ans);
     
  }
 
}
 
// test string
let test =  "helloworld";
count_occurs( test ,count);
 
</script>

 
Output:

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times

Approach 2: In this approach, we use nested for loop to iterate over string and count for each character in the string. 

  • First initialize count with value 0 for ith value of string.
  • Now we iterate over string if ith value matches with the character, increase the count value by 1.
  • Finally, print the value of count.

Example: 

Javascript




<script>
 
// function that count character occurrences in string
function count_occur( str )
{
  // checking string is valid or not
  if( str.length == 0 )
  {
    console.log("Invalid string")
    return;
  }
  //cor loop to iterate over string
  for( let i = 0 ;i < str.length ;i++)
  {
    //variable counting occurrence
    let count = 0;
    for( let j = 0 ;j < str.length ;j++)
    {
      if( str[i] == str[j] && i > j  )
      {
       break;
      }
      if( str[i] == str[j]  )
      {
        count++;
      }
    }
    if( count > 0)
    console.log(`${str[i]} occurs ${count} times`);
     
  }
 
}
 
// test string
let test_str = "gfghello";
count_occur( test_str);
</script>

 
Output:

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times

Approach-3: In this approach we will use the for loop in order to iterate over the complete string itself. This is the simplest approach we may implement in order to find out the resulted task.

  • We will initialize a count variable variable which will store the count of each and every character in a string.
  • Then using a for loop we will check how many times a character in string has been occurred or repeated or present.

Javascript




// JavaScript code for the above approach...
 
let countCharacters = (string) => {
  let count = 1;
  for (let i = 0; i < string.length; i++) {
    if (string[i] === string[i + 1]) {
      count++;
    } else {
      console.log(`${string[i]} occur ${count} times`);
      count = 1;
    }
  }
};
 
countCharacters("hello");
 
// This code is contributed by Aman Singla...

Output:

h occur 1 times
e occur 1 times
l occur 2 times
o occur 1 times

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!