Problem 34
Minimize zeroes Recursion Challenge
Given an array of numbers, return a new array where each consecutive run of zeroes is reduced to a single zero. Non-zero values and separate groups of zeroes should each retain one zero.
Function Signature
minimizeZeroes(array)
Parameters
array— an array of numbers.
Output
Return a new array with consecutive duplicate zeroes collapsed.
Constraints
- The function must use recursion.
- The input array must not be mutated.
- The function should accept exactly one argument.
Examples
minimizeZeroes([2, 0, 0, 0, 1, 4]) → [2, 0, 1, 4]
minimizeZeroes([2, 0, 0, 0, 1, 0, 0, 4]) → [2, 0, 1, 0, 4]