code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

283 Move Zeros

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/*
 * Given an array nums, write a function to move all 0's to the end of it
 * while maintaining the relative order of the non-zero elements.
 *
 * Example:
 *
 * Input: [0,1,0,3,12]
 * Output: [1,3,12,0,0]
 * Note:
 *
 * You must do this in-place without making a copy of the array.
 * Minimize the total number of operations.
 */

function moveZeros(nums) {
  let start = 0
  let end = nums.length
  while (start < end) {
    if (nums[start] === 0) {
      nums.splice(start, 1)
      nums.push(0)
      end--
    } else {
      start++
    }
  }
  return nums
}

test('move Zeros', () => {
  expect(moveZeros([0, 1, 0, 3, 12])).toEqual([1, 3, 12, 0, 0])
  expect(moveZeros([1, 0])).toEqual([1, 0])
  expect(moveZeros([0, 0, 1])).toEqual([1, 0, 0])
})

Created 2020-04-11T11:10:20+00:00 · Edit