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.
Zero elements are defined by either 0 or "0". Some tests may include elements that are not number literals.
NOT allowed to use any temporary arrays or objects. Also not allowed to use any Array.prototype or Object.prototype methods.So no array.push or splice() is allowed.
I tried this:
function removeZeros(nums) {
for(let i = nums.length - 1; i >= 0 ; i--){
if(nums[i] === 0){
nums.splice(i, 1);
nums.push(0);
}
}
return nums;
}
input:
[7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14]
Expected:
[7, 2, 3, 4, 6, 13, 78, 19, 14, 0, 0, 0, 0, 0, 0]
Actual:
[7, 2, 3, 4, 6, 13, 78, 19, 14, 0, 0, 0, 0, 0, 0]
Result is coming as correct but I need to do it without array.push() or array.splice()