Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda