1

How to loop through all characters in a string and count the number of occurrences of each character and populate an object with the results? I came across this on Codewars and didn't find the answer specifically structured this way here on stackoverflow, and so I wanted to share my solution. I hope this is going to be helpful for someone out there.

Example input: "Apple" Output: {A: 1, p: 2, l: 1, e: 1}

Developing my solution, I needed to find out how to create a dynamic object using a for loop. And so, I found this to be particularly helpful:

https://stackoverflow.com/a/69328904/13200699

But, then I had to make a few additions to it, because my challenge was a bit different than that. And finally I came up with the solution below;

  • 1
    Welcome to SO. However this site isn't a suitable forum for sharing working code you've written. It's a site that helps developers with specific issues they have with their code. Particularly since this is already a solved problem with (probably) many duplicates. – Andy Jun 09 '23 at 13:13

2 Answers2

1

You can do it more elegantly like this, using reducer:

const string = "Apple";

let obj = string.split('').reduce((acc, char) => {
  acc[char] = (acc[char] || 0) + 1;
  return acc;
}, {});

console.log(obj);
  • Hi Ognjen. Thanks for your contribution. But, unfortunately, I couldn't get your code working. I don't knnow what is not right about it, but, it doesn't return the expected result: {A: 1, p: 2, l: 1, e: 1} PS. I added "let obj = {}" to the beginning of your code, but it still didn't work. – Ubeydullah Keleş Jun 09 '23 at 17:14
  • Sorry for the mistake. Here is the working code. – Ognjen Stefanovic Jun 12 '23 at 06:43
  • Thanks. Could you please elaborate on the code? Especially the 4th line; acc[char] = (acc[char] || 0) + 1; – Ubeydullah Keleş Jun 12 '23 at 09:21
  • 1
    You can read more about reducers here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce The second parameter in the reducer is the initial value, which in this case is an empty object `{ }`. So, initially, since it's empty, there are no defined keys. 4th line is the increment code: if it is the first appearance of a character then acc[char] is undefined, so we take the 0 as value, and in every other appearance, that value will be defined, so we just increment that number, and return the whole object for the next iteration of the reducer. – Ognjen Stefanovic Jun 12 '23 at 11:29
  • 1
    acc[char] = (acc[char] || 0) + 1 This `OR` operator (`||`) is using so-called short-circuiting, which you can read more about here: https://codeburst.io/javascript-what-is-short-circuit-evaluation-ff22b2f5608c – Ognjen Stefanovic Jun 12 '23 at 12:25
0

Here is the solution I developed:

const string = "Apple"
let obj = {}

for( let i = 0 ;i < string.length ;i++) {
    //variable counting occurrence
    let count = 0;
    for(let j = 0 ;j < string.length ;j++) {
        if( string[i] == string[j] && i > j  ) {
        break;
        }
        if(string[i] == string[j]) {
        count++;
        obj[string[i]] = count // This is where I am populating the object.
        }
    }
}

console.log(obj)