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

190
Views
Count the number of repeated letters from the array of each string

i have an array of strings with duplicated letters , Now i want to count the number of duplicated letters from each string and need to create a separate object for each string. The given array :

let ArrOfStrs = ["aaabbbccc", "dddeeefff", "ggghhhiii", "jkl"]

what i have tried :

var obj={}
var repeats=[];
for(let i=0; i< ArrOfStrs.length; i++) {
    for(let x = 0;  x < ArrOfStrs[i].length; x++) {
        var l = ArrOfStrs[i].charAt(x);
        obj[l] = (!obj.hasOwnProperty(l) ? 1 : obj[l] + 1);
    }
}
repeats.push(obj);
console.log(repeats); 

Actual output :

 { a: 3, b: 3, c: 3, d: 3, e: 3, f: 3, g: 3, h: 3, i: 3, j: 1, k: 1, l: 1 } 

Expected output :

 [{a:3,b:3,c:3}, {d:3,e:3,f:3},{g:3,h:3,i:3},{j:1,k:1,l:1}] 

Could some one help for better approach to achieve this. Thanks in advance.

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  • Array.prototype.map to create a new array from an existing one
  • Array.prototype.reduce to convert an array to a different type (Object)
  • Spread syntax ... to convert String to Array

const arrOfStrs = ["aaabbbccc", "dddeeefff", "ggghhhiii", "jkl"];

const counted = arrOfStrs.map(str => [...str].reduce((o, v) => {
  o[v] ??= 0    // set default count to 0 (if not exists)
  o[v]++        // increment count
  return o;
}, {}));

console.log(counted);

Additional read:

  • Nullish coalescent operator ??

Variant:

const arrOfStrs = ["aaabbbccc", "dddeeefff", "ggghhhiii", "jkl"];
const counted = arrOfStrs.map(s => [...s].reduce((o, v) => (o[v] ??= 0, o[v]++, o), {}));

console.log(counted);

almost 4 years ago · Santiago Trujillo Report

0

2 mistakes :

  1. You are keeping a common object obj and making changes to it. Infact obj will be different for every string.
  2. You are not pushing obj into your result array for every iteration. You are doing it in the end, hence you have only one item in your array.

Making minimal changes, your code can be fixed:

let ArrOfStrs = ["aaabbbccc", "dddeeefff", "ggghhhiii", "jkl"]

var repeats=[];
for(let i=0; i< ArrOfStrs.length; i++) {
    var obj={}
    for(let x = 0;  x < ArrOfStrs[i].length; x++) {
        var l = ArrOfStrs[i].charAt(x);
        obj[l] = (!obj.hasOwnProperty(l) ? 1 : obj[l] + 1);
    }
    repeats.push(obj);
}

console.log(repeats);

almost 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!