In this article, we are going to implement a JavaScript program to print the frequency of each character in alphabetical order. This operation involves counting how many times each character (typically, alphabetic characters) appears in a given text and then displaying the results in alphabetical order.
Example:
Input: str = “aabccccddd”
Output: a2b1c4d3
Since it is already in alphabetical order, the frequency
of the characters is returned for each character.
Input: str = “geeksforgeeks”
Output: e4f1g2k2o1r1s2
Table of Content
Using Regular Expression
In this approach, we are using regular expressions to remove any non-alphabetic element from the string, then storing the alphabet and their respective frequencies in an object called frequency.
Syntax:
const cleanedString = inputString.replace(/[^a-z]/gi, '').toLowerCase();
const printAlphabeticalFrequency = ( inputString ) => {
// Remove non-alphabetic characters
// and convert to lowercase
const cleanedString = inputString
.replace(/[^a-z]/gi, "" )
.toLowerCase();
// Create an object to store the
// character frequencies
const frequency = {};
// Iterate through the cleaned string
for (let char of cleanedString) {
// If the character is not in the
// frequency object, add it with a
// count of 1
if (!frequency[char]) {
frequency[char] = 1;}
else {
// If the character is already
// in the frequency object,
// increment its count
frequency[char]++;
}}
// Get an array of the characters
// and sort them in alphabetical order
const characters =
Object.keys(frequency).sort();
// Iterate through the sorted characters
// and print their frequencies
for (let char of characters) {
console.log(
char + ": " +
frequency[char]
);}
}; const inputString = "Geeks for Geeks" ;
printAlphabeticalFrequency(inputString); |
e: 4 f: 1 g: 2 k: 2 o: 1 r: 1 s: 2
Using a match() Method
In this approach, we are using the match method of JavaScript. It is an inbuilt method of JavaScript to search a string for a match against any regular expression. If the match is found, then this will return the match as an array.
Syntax:
const matches = cleanedString.match(new RegExp(char, 'g'));
const printAlphabeticalFrequency = ( inputString ) => {
// Remove non-alphabetic characters
// and convert to lowercase
const cleanedString = inputString
.replace(/[^a-z]/gi, "" )
.toLowerCase();
// Create a string of all lowercase alphabets
const alphabet =
"abcdefghijklmnopqrstuvwxyz" ;
// Initialize an object to store
// character frequencies
const frequency = {};
// Iterate through the alphabet
for (let char of alphabet) {
// Use the match() function with a
// regular expression to find all occurrences
// of the character
const matches =
cleanedString.match(
new RegExp(char, "g" ));
if (matches) {
// If matches are found, store the
// count in the frequency object
frequency[char] =
matches.length;
}}
// Iterate through the characters in
// alphabetical order and print their frequencies
for (let char of alphabet) {
if (
frequency[char] !==
undefined) {
console.log(
char + ": " +
frequency[char]
);
}}}; const inputString = "GeeksForGeeks" ;
printAlphabeticalFrequency(inputString); |
e: 4 f: 1 g: 2 k: 2 o: 1 r: 1 s: 2