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

121
Visualizações
how to find the number of repeated strings

const repeat= (nums) =>{ //* Done
    let ans = []
    for(let i = 0; i< nums.length; i++){
      if(nums[i] === nums[i+1]){
         if(ans[ans.length -1] !== nums[i]){
            ans.push(nums[i])
         }
      } 
    }
    return ans.length 
}

console.log(repeat(['nsmg33de1','nsmg33de1','2211,','2211','1234','1234']))

in this example it seems this function wont work properly there is 3 repeated string in the array but it will output 2

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

[I hope this will help you, Firstly convert the given string to an array. To do that use string.split(""). Secondly, create an map which will store word as key and count as value.

Now iterate through the stringArray and store the current word to the map. And increase the count for the word each time the word is found.

Check the below link ][1]

let words = "I am not gonna live forever, but I wanna live while I am alive";

function countRepeatedWords(sentence) {
  let words = sentence.split(" ");
  let wordMap = {};

  for (let i = 0; i < words.length; i++) {
    let currentWordCount = wordMap[words[i]];
    let count = currentWordCount ? currentWordCount : 0;
    wordMap[words[i]] = count + 1;
  }
  return wordMap;
}

console.log(countRepeatedWords(words));

about 4 years ago · Santiago Trujillo Relatório

0

Depends on what value you want? If you want the amount of duplications that have occurred, you could convert your list into a Set to remove all duplications and just calculate the difference of sizes between this set and the original list like this:

function repeat(values) {
  return values.length - new Set(values).size;
}

Otherwise if you want to know how many items there were which had at least 1 duplicate that would be a different story.

For this you could potentially convert the set to an array and then map a 1 at every value where the value is found more than once in the array and a 0 for all the others. Afterwards you could reduce this array by adding all of these values together. Like this:

function repeat(values) {
  return [...new Set(values)].map(v => values.filter(o => o == v).length > 1 ? 1 : 0).reduce((a, b) => a + b);
}
about 4 years ago · Santiago Trujillo Relatório

0

I threw this together in the console. Not the best algorithm in the world, but I think this is what you are trying to do:

const getCountOfDuplicates = (items) => {
   const count = {};
   
   items.forEach(item => {   
     if(count[item] && count[item] > 0) {
        count[item]++;
     } else {
        count[item] = 1;
     }
   });

    return Object.values(count).reduce((acc, value) => {
        if(value > 1) {
        acc++;
      }
      
      return acc;
    }, 0);
};

console.log('results 1', getCountOfDuplicates(['1','2','3','4','1','2','3','5', '6', '6', '7'])); // Output: "results 1", 4
console.log('results 2', getCountOfDuplicates(['nsmg33de1','nsmg33de1','2211,','2211','1234','1234'])) // Output: "results 2", 2

about 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