Horje
JavaScript Program to Count the Occurrences of Each Character

This article will demonstrate different approaches for implementing a JavaScript program to count the occurrences of each character in a string. We will be given a string as input and we return the chars used with the number it occurred in that string.

Approaches to count the occurrences of each character

Method 1: Using JavaScript Objects

In this method, we will create a JavaScript object that will store all the characters and their occurrences to accomplish the task.

Example: In this example, we will store chars and their occurrences in the result object using for loop for iteration.

JavaScript
let str = 'GeeksforGeeks'

let result = {}
for(let i = 0;i< str.length;i++){
  let ch = str.charAt(i)
  if(!result[ch]){
    result[ch] =1;
  }
  else{
    result[ch]+=1
  }
}
console.log(
    "The occurrence of each letter in given string is:",result)

Output
The occurrence of each letter in given string is: { G: 2, e: 4, k: 2, s: 2, f: 1, o: 1, r: 1 }

Method 2: Using JavaScript Map

In this method, JavaScript map is used so that we can store the result in thje form of key-value pairs where key is the chars used and value will be the number of occurrences.

Example: In this example, we will iterate the given string and store result in the form of key-value pairs.

JavaScript
let str = 'GeeksforGeeks'

let result = new Map()
for(let i = 0;i< str.length;i++){
  let ch = str.charAt(i)
  if (!result.get(ch)) result.set(ch, 1);
    else {
        result.set(ch, result.get(ch) + 1);
    }
}
console.log(result)
// console.log(
    // "The occurrence of each letter in given 
    // string is:",result)

Output
Map(7) {
  'G' => 2,
  'e' => 4,
  'k' => 2,
  's' => 2,
  'f' => 1,
  'o' => 1,
  'r' => 1
}

Method 3: Using JavaScript Array

In this method, we will use JavaScript Array to get the chars and occurrences by converting the string in array and then apply JavaScript array methods.

Example: In this example, we will use array reduce to return an array that contains the chars and their occurrences.

JavaScript
let str = "GeeksforGeeks";

let strArray = str.split("");

// console.log(strArray)
let result = strArray.reduce((chars, ch) => {
    if (!chars[ch]) {
        chars[ch] = 1;
    } else {
        chars[ch] += 1;
    }
    // console.log(ch);
    return chars;
}, []);

// console.log(result)
console.log(
    "The occurrence of each letter in given string is:", result)

Output
The occurrence of each letter in given string is: [
  G: 2, e: 4,
  k: 2, s: 2,
  f: 1, o: 1,
  r: 1
]

Method 4: Using JavaScript forEach() Method with an Object

In this method, we will iterate over each character of the string using the forEach() method. We’ll use an object to store the count of each character.

Example:

JavaScript
let str = "GeeksforGeeks";
let result = {};

// Iterate over each character of the string
str.split("").forEach(char => {
    // Increment the count of the character in the object
    result[char] = (result[char] || 0) + 1;
});

console.log("The occurrence of each letter in the given string is:", result);

Output
The occurrence of each letter in the given string is: { G: 2, e: 4, k: 2, s: 2, f: 1, o: 1, r: 1 }

Method 5: Using JavaScript reduce() Method with an Object

In this method, we’ll use the reduce() method directly on the array obtained from splitting the string. This will accumulate the character counts in an object.

JavaScript
let str = "GeeksforGeeks";

let result = str.split("").reduce((acc, char) => {
    acc[char] = (acc[char] || 0) + 1;
    return acc;
}, {});

console.log("The occurrence of each letter in the given string is:", result);

Output
The occurrence of each letter in the given string is: { G: 2, e: 4, k: 2, s: 2, f: 1, o: 1, r: 1 }

Method 6: Using JavaScript for…of Loop

In this method, we’ll use the for…of loop to iterate over each character in the string and use an object to keep track of the occurrences of each character.

Example: This approach is simple and leverages the for…of loop to iterate through each character of the string, making it a straightforward and easy-to-understand method for counting character occurrences.

JavaScript
let str = "GeeksforGeeks";
let result = {};

// Iterate over each character of the string using for...of loop
for (let char of str) {
    // Increment the count of the character in the object
    result[char] = (result[char] || 0) + 1;
}

console.log("The occurrence of each letter in the given string is:", result);

Output
The occurrence of each letter in the given string is: { G: 2, e: 4, k: 2, s: 2, f: 1, o: 1, r: 1 }






Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Find if a Character is a Vowel or Consonant JavaScript Program to Find if a Character is a Vowel or Consonant
JavaScript Program to Update the Value for a Specific Key in Map JavaScript Program to Update the Value for a Specific Key in Map
JavaScript Program to Convert Date to String JavaScript Program to Convert Date to String
JavaScript Program to Print Square Star Pattern JavaScript Program to Print Square Star Pattern
JavaScript Program to Check if a Given Integer is Positive, Negative, or Zero JavaScript Program to Check if a Given Integer is Positive, Negative, or Zero

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11