-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoveZeros.js
More file actions
41 lines (32 loc) · 1.59 KB
/
Copy pathremoveZeros.js
File metadata and controls
41 lines (32 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Created by D on 3/4/2015.
*/
/*
Write a function that takes an array of values and moves all elements that are zero to the end of the array, otherwise
preserving the order of the array. The zero elements must also maintain the order in which they occurred.
For example, the following array
[7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14]
is transformed into
[7, 2, 3, 4, 6, 13, 78, 19, 14, 0, 0, 0, 0, 0, 0]
Zero elements are defined by either 0 or "0". Some tests may include elements that are not number literals.
You are NOT allowed to use any temporary arrays or objects. You are also not allowed to use any Array.prototype or
Object.prototype methods.
*/
function removeZeros(array) {
if (array.length === 0) return array;
var zeroCount = 0; //keeps track of zeros so we know when to exit for loop
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] === 0 || array[i] === '0') { //shuffle numbers back one space and put zero at end
zeroCount += 1;
for (var j = i + 1; j < len; j++) {//this looks for non-zero after i of array
var itemToMoveUp = array[j]; //non-zero number at end to swap places with zero num
array[j] = array[j-1]; //set index of num we will move up to zero
array[j-1] = itemToMoveUp; //now the zero has been swapped for a non-zero
}
i -= 1; //move index back one because next item is now one index lower
}
if (i + 1 + zeroCount >= len){ //if only zeros that were moved are left -> end
return array;
}
}
}