Anjan Dutta

Leet Code 283: Move Zeroes

Leet Code 283: Move Zeroes

Problem statement:

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Constraints:

  • 1 <= nums.length <= 104
  • -231 <= nums[i] <= 231 - 1

Soultion:

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
let l = 0;
let r = nums.length;
// if(r==1)
// return;
let arr = [];
for(let i=0;i<r;i++) {
if(nums[i] !== 0 && i!==l){
nums[l] = nums[i];
nums[i] = 0;
l++;
} else if(nums[i] !== 0 && i==l) {
l++;
}
}
console.log(nums);
};