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

322
Views
JavaScript Trying to Print The Number of Times a Letter Appears In String But It Prints More than Once

In the code below, I am trying to check how many times a letter in a string appears. Problem with the code below is that it prints each letter more than once. It needs to collect all the same letters and show the number of times it occurs in the string and display it once.

const string = 'mississippi'

const letters = [...string]

let currentLetter = ''
let letterOccurance = []


for(let i = 0; i < letters.length; i++){
  let letterFrequency = letters.filter((letter)=>{
    return letter === letters[i]
  })

  letterOccurance.push([`${letters[i]}`,letterFrequency.length])
}

console.log(letterOccurance)

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

That's too much code just to get the number of times a letter appears in a string. Try the following code:

const string = 'mississippi';

let frequency = {};

for (let letter of string) {
  if (frequency[letter]) {
    frequency[letter]++;
  } else {
    frequency[letter] = 1;
  }
}

console.log(frequency);

about 4 years ago · Santiago Trujillo Report

0

You're always pushing the letter to the array, whether it already exists there or not:

letterOccurance.push([`${letters[i]}`,letterFrequency.length])

You could check if it exists first:

if (!letterOccurance.find(l => l[0] === letters[i])) {
  letterOccurance.push([`${letters[i]}`,letterFrequency.length])
}

Or even skip it entirely if you've already seen it, since the first time you find any letter you already know its complete count:

for(let i = 0; i < letters.length; i++){
  if (letterOccurance.find(l => l[0] === letters[i])) {
    continue;
  }
  // the rest of the loop...
}

There's honestly a variety of ways you could step back and re-approach the problem. But for the question about why letters are repeating, that's simply because each iteration of the loop unconditionally appends the letter to the resulting array.

about 4 years ago · Santiago Trujillo Report

0

How about writing a more generic item-counting function and then layering countLetters as a simple partial application of the identity function?

const countBy = (fn) => ([...xs]) =>
  xs .reduce ((a, x) => {const k = fn (x); a [k] = (a[k] || 0) + 1; return a}, {})

const countLetters = countBy (x => x)

console .log (countLetters ('missisippi'))

countBy is fairly generic. You pass it a function to convert your values to strings, and pass your array of items to the function it returns. Strings are array-like enough that this just works for our simple countLetters. But we could use it for other counts as well, such as:

countBy (x => x .grade) ([{id: 1, grade: 'A'}, {id: 2, grade: 'B'}, {id: 3, grade: 'A'}])
//=> {"A": 2, "B": 1}
about 4 years ago · Santiago Trujillo 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!