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

1089 Duplicate Zeros

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

/*
 * Given a fixed length array arr of integers, duplicate each occurrence of zero,
 * shifting the remaining elements to the right.
 *
 * Note that elements beyond the length of the original array are not written.
 *
 * Do the above modifications to the input array in place, do not return anything
 * from your function.
 *
 * Example 1:
 * Input: [1,0,2,3,0,4,5,0]
 * Output: null
 * Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
 */

function duplicateZeros(value) {
  let queue = []
  for (let index = 0; index < value.length; index++) {
    const current = value[index]
    if (queue.length) {
      queue.push(current)
      value[index] = queue.shift()
    }
    if (current === 0) {
      queue.push(current)
    }
  }
  return value
}

test('duplicate Zeros', () => {
  expect(duplicateZeros([1, 0, 2, 3, 0, 4, 5, 0])).toEqual([1, 0, 0, 2, 3, 0, 0, 4])
})

Created 2020-04-20T11:33:53+00:00, updated 2020-04-20T12:19:37+00:00 · History · Edit