Problem 32

Eliminate consecutive duplicates Recursion Challenge

Given an array, return a new array with consecutive duplicate values collapsed into a single occurrence. Non-consecutive duplicates should remain.

Function Signature

compress(list)

Parameters

  • list — an array of values.

Output

Return a new array with consecutive duplicates removed.

Constraints

  • The function must use recursion.
  • The input array must not be mutated.
  • The function should accept exactly one argument.

Examples

compress([1, 2, 2, 3, 4, 4, 5, 5, 5])       → [1, 2, 3, 4, 5]
compress([1, 2, 2, 3, 4, 4, 2, 5, 5, 5, 4, 4]) → [1, 2, 3, 4, 2, 5, 4]