0

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?

4
  • When you splice, that skips an index on the next iteration because arr.length is one shorter -- all of the elements after i have moved 1 forward to fill its spot. Use i-- whenever you splice or loop in reverse. Also, splice here is O(n), so this is an unnecessarily quadratic solution. See the linked dupe suggestion for a variety of linear solutions. Commented Jan 14, 2022 at 19:48
  • arrThree.filter(i => i != 0).concat(arrThree.filter(i => i == 0)) Commented Jan 14, 2022 at 19:54
  • But if you want to fix your code, you could get rid of the test array and do: if (arr[i] === 0) arr.push(arr.splice(i,1)[0]); This will not change the length so the for loop still works. Commented Jan 14, 2022 at 19:56
  • @JohnnyMopp do you mind adding this two options as answers with some comments? Commented Jan 14, 2022 at 20:01

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

Comments

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]));

Comments

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));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.