Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda