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

211
Vistas
How to remove repeated number from an array?

How to completely remove repeated numbers from an array?

For example, if:

const array = [1, 1, 2, 3, 1, 2, 5]

The output should be:

[3, 5]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Roko and Ke1vans had answered in functional approaches. Both of them are correct. However, I'd show an answer in imperative approach, which may seems easier for new comer.

Similar to their flow. First, we count the occurrence of each number. Then we select the numbers that has occurred once (hence being non-repeated) into the output array.

let array = [1,1,2,3,1,2,5]
let counts = {}
let output = []

// loop each elements in the array as `item`
for(let item of array) {
  // If the item is not set in the counts, `counts[item]` will be `undefined`.
  // Using `|| 0` means use zero as fallback value if the items is unseen.
  let count = counts[item] || 0
  counts[item] = count + 1
}

// loop each keys in the object (key-value pairs) as `item`
for(let item in counts) {
  let count = counts[item]
  if(count == 1) {
    // `+item` converts the key from string into number
    output.push(+item)
  }
}

console.log(output) // will print out `[ 3, 5 ]`
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can iterate and create a map of values. later iterate and filter.

const data = [1, 1, 2, 3, 1, 2, 5];

const findUniques = (data = []) => {
  const map = data.reduce((m, num) => {
    m[num] = (m[num] || 0) + 1;
    return m;
  }, {});
  return data.filter((num) => map[num] === 1);
};

console.log(findUniques(data));

You can also do the same using 2 set, or 2 array.

const data = [1, 1, 2, 3, 1, 2, 5];
const findUniques2 = (data = []) => {
  let unique = new Set();
  let seen = new Set();
  for (let num of data) {
    if (seen.has(num)) unique.delete(num);
    else unique.add(num);
    seen.add(num);
  }
  return Array.from(unique);
};
console.log(findUniques2(data));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use an Object where the key is the number, and the value is the number of occurrences. Than reduce it back to the desired array of values:

const arr = [1,1,2,3,1,2,5];

const res = Object.entries(arr.reduce((ob, v) => {
  if (!(v in ob)) ob[v] = 0;
  ob[v] += 1;                       // Count occurrences
  return ob;
}, {})).reduce((arr, [k, v]) => {   // Reduce back to Array
  if (v === 1) arr.push(+k);        // Only keys with 1 occurrence
  return arr;
}, []);

console.log(res);                   // [3, 5]

about 4 years ago · Juan Pablo Isaza 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