Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created December 3, 2019 11:52
Show Gist options
  • Save Fhernd/3f19ce1e347b8fe1b3c68ff4bdf2f3f0 to your computer and use it in GitHub Desktop.
Save Fhernd/3f19ce1e347b8fe1b3c68ff4bdf2f3f0 to your computer and use it in GitHub Desktop.
// Ejercicio 191: Contar las ocurrencias de los elementos de un arreglo con forEach.
// Ejercicio 191: Contar las ocurrencias de los elementos de un arreglo con forEach.
// Solución:
let numeros = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6, 7, 8];
let conteo = {};
numeros.forEach((valor) => {
if(conteo[valor]){
conteo[valor] += 1;
} else {
conteo[valor] = 1;
}
});
console.log(numeros);
console.log(conteo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment