Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Count the occurrence of every alphabet from a string in Javascript

I have seen similar questions like this asked before, such as counting characters in a given string. However when it comes to comparing given string to the letters of the alphabet and returning an object with occurences such as:

const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

const sampleString = "a bee";

const results = {
 a: 1,
 b: 1,
 c: 0,
 d: 0,
 e: 2,
 f: 0,
 ...
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

We can use Array.reduce(), to count letters in the sampleString.

We start by creating a letterMap to specify all valid letters to be counted.

In the reduce loop, we only increment letters that are present in the letterMap, using the (c in acc) expression.

const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

const sampleString = "a bee";

const letterMap = letters.reduce((acc, c) => { 
    acc[c] = 0; 
    return acc; 
}, {});

const result = [...sampleString].reduce((acc, c) => {
    if (c in acc) acc[c]++;
    return acc;
}, letterMap);

console.log('Result:', result)
 
.as-console-wrapper { max-height: 100% !important; top: 0; }

Here's another way, using just one loop, again using Array.reduce(), this assumes we don't wish to count whitespace:

const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

const sampleString = "a bee";

const result = [...letters, ...sampleString].reduce((acc, c) => {
    if (c in acc) { 
        acc[c]++;
    } else if (c.trim()) {
        acc[c] = 0;
    }
    return acc;
}, {});

console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

I like using Object.fromEntries for this:

const sampleString = "a bee";
const result = Object.fromEntries(Array.from("abcdefghijklmnopqrstuvwxyz", ch => [ch, 0]));
for (let ch of sampleString) 
    if (ch in result) result[ch]++;
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Using Array.reduce and String.match may be an idea. So, for each letter of letters, use match (length) to determine the frequency of the letter in the given sample.

const letters = `abcdefghijklmnopqrstuvwxyz`.split(``);
const freq = (chr, sample) => (sample.match(RegExp(chr, `g`)) || []).length;
const result = letters.reduce( (acc, chr) => 
  ({...acc, [chr]: freq(chr, acc.sample)}), {sample: "a bee"});
console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!