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

144
Vistas
Remove particular array elements and push them to the back of the array

Trying to remove all 0's from an array and return them in the same array

So for example if I have this as an array

let arrThree = [9,0,9,1,2,1,1,3,1,9,0,0,9,0,0,0,0,0,0,0]

I would get this:

let arrThree = [9,9,9,1,2,1,1,3,1,9,9,0,0,0,0,0,0,0,0,0,0,0]

This is what I wrote:

var remove = function (arr) {
  let test = [];
  for(let i = 0; i < arr.length; i++){
    arr[i] === 0 ? test.push(arr.splice(i,1)) : false //if i is 0 then we want to push that into another array
  }
  arr.push(...test)
  return [].concat(...arr) 
}

When I run this function I get this

[
  9, 9, 1, 2, 1, 1, 3,
  1, 9, 0, 9, 0, 0, 0,
  0, 0, 0, 0, 0, 0
]

Not sure where I am going wrong?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The issue with your code is you change the length of arr with arr.splice(i,1). This messes up the loop condition i < arr.length.

To fix your code, you can loop backwards through the array. So as the length of the array shortens, i is still valid.

let arrThree = [9,0,9,1,2,1,1,3,1,9,0,0,9,0,0,0,0,0,0,0];

var remove = function(arr) {
  let test = [];
  for(let i = arr.length - 1; i >= 0; --i) {
    if (arr[i] === 0) test.push(arr.splice(i, 1)[0]);
  }
  return arr.concat(test);
}

console.log(remove(arrThree));

There are a few other ways to do this. One is to use filter to create 2 arrays - one with no 0 and one with all 0 and join them:

let arrThree = [9,0,9,1,2,1,1,3,1,9,0,0,9,0,0,0,0,0,0,0];

let remove = (arr) => arr.filter(i => i != 0).concat(arr.filter(i => i == 0));

console.log(remove(arrThree));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need only two iterations, one for finding non zero values and another to put zeroes to the right side until end of the array. You need no array pushing or splicing.

const
    move0 = array => {
        let i = 0,
            j = 0;
            
        while (i < array.length) {
            if (array[i]) array[j++] = array[i];
            i++;
        }
        while (j < array.length) {
            array[j] = '#'; // in reality it is zero
            j++;
        }
    },
    array = [9, 0, 9, 1, 2, 1, 1, 3, 1, 9, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0];

move0(array)
console.log(...array);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you don't need to have any sorting on the non-zero elements, a rather efficient solution would be to swap the left-most 0 with the right-most non-zero element. Once your indices for tracking where you are on the left and right cross you'll know you're done.

function moveZeros(arr) {
  let i = 0;
  let j = arr.length - 1;
  
  while(i < j) {
    // Found a 0 to the left of a non-zero, swap.
    if(arr[i] == 0 && arr[j] != 0) {
      let tmp = arr[j];
      arr[j] = arr[i];
      arr[i] = tmp;
    }

    // Find a zero
    if(arr[i] != 0) {
      i++;
    }
    
    // Find a non-zero
    if(arr[j] == 0) {
      j--;
    }
  }
  
  return arr;
}

console.log(moveZeros([1,2,0,3,0,0,0]));
console.log(moveZeros([9,0,9,1,2,1,1,3,1,9,0,0,9,0,0,0,0,0,0,0]));

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