Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!