# Javascript Algorithm Practice Removing Negatives from Array

It's hard for me to wrap my brain around this: /

- (Removing negatives). Given an array X with multiple values ββ(eg [-3,5,1,3,2,10]), write a program that removes any negative values ββin the array. Once your program is done, X should only be positive numbers.
**Do this without creating a temporary array and only using the pop method to remove any values ββin the array.**

My thought was to write a loop through the array. If X [i] is negative, start another loop, replacing X [j] and X [j + 1] to the end of the array. (to preserve the order of the array) use the pop () function.

When I run the script it looks like the loop is infinite. It also looks like if there are two negative values ββin the string, the second can be skipped on the next iteration i. Is there an easier way?

```
var X = [1,-6,-7,8,9];
//test= [1,-7,8,-6,9]
temp = 0
for (i = 0;i<X.length-1;i++){
if (X[i]<0){
for (j = i;j<=X.length-1;j++){
X[j] = temp
X[j] = X[j+1]
X[j+1] = temp
}
if(X[X.length-1] < 0){X.pop()}
}
};
console.log(X);
```

source to share

Very similar to your mentioned approach, except in cases where there is no need to maintain order (if not stated in the description). Loop **in reverse** , and when minus is found, replace it with the last element and pop. If we lay out all the negatives from the end first, we know that the last element is not negative.

```
var x = [1, -6, -7, 8, 9, -3];
// strip all negatives off the end
while (x.length && x[x.length - 1] < 0) {
x.pop();
}
for (var i = x.length - 1; i >= 0; i--) {
if (x[i] < 0) {
// replace this element with the last element (guaranteed to be positive)
x[i] = x[x.length - 1];
x.pop();
}
}
document.body.innerHTML = '<pre>' + JSON.stringify(x, null, 4) + '</pre>';
```

This solution has linear complexity as it only iterates through the list.

source to share

Sort the array first so that negative numbers come at the end.

We can sort with a callback that moves negative numbers to the end.

Then move back and remove the last indices from `pop`

while they are negative.

We are left with positive values.

```
var X = [-3, 5, 3, 8, 1,-6,-7,8,9];
X.sort(function(a,b) {
return b - a;
});
for (var i=X.length; i--;) {
if ( X[i] < 0 ) X.pop();
}
document.body.innerHTML = '<pre>' + JSON.stringify(X, null, 4) + '</pre>';
```

source to share

There are already many good answers. Here's a simple filter that doesn't sort the array and uses the sub-array index `j <= i`

:

```
function removeNeg(arr) {
var j = 0;
// filter array
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= 0) arr[j++] = arr[i];
}
// pop excess elements
while (j < arr.length) arr.pop();
}
```

This is indeed the C programmer's approach to James Montagne's answer, which is neater because it appears as you go.

source to share

Here's a very simple solution that doesn't require sorting. For each item, slide it, click if not negative. Do this multiple times, equivalent to the size of the array. This can be done with shift / push or pop / shift.

```
var origLen = X.length;
for(var i = 0; i < origLen; i++) {
var val = X.pop();
if(val > 0)
X.unshift(val);
}
```

source to share