Duplicate Zeros in Array
In this lab, you will be working with a fixed-length integer array called
arr. Your task is to duplicate each occurrence of zero in the array while shifting the remaining elements to the right. Keep in mind that elements beyond the length of the original array should not be written. You must carry out the modifications to the input array in place and not return anything.
You are provided with an integer array
arr. Your task is to implement a function called
duplicateZeros that takes
arr as its input and duplicates each occurrence of zero, shifting the rest of the elements to the right. Note that elements beyond the length of the original array should not be written, and the modifications should be done in place without returning anything.
Here are a few examples to illustrate the expected behavior:
Input: arr = [1,0,2,3,0,4,5,0] Output: [1,0,0,2,3,0,0,4] Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
Input: arr = [1,2,3] Output: [1,2,3] Explanation: After calling your function, the input array is modified to: [1,2,3]
1 <= arr.length <= 104
0 <= arr[i] <= 9
duplicateZerosfunction that duplicates each occurrence of zero in the given array and shifts the remaining elements to the right. Export this function using ESM import/export syntax. Your implementation should follow the constraints mentioned above.
Your implementation should pass the given test cases. Use surrounding test cases to verify if your implementation is working as expected.
NOTE: Make sure to default export the function.