Problem 48

Subsets Recursion Challenge

Given an array, return an array containing every possible subset (the power set). The order of the subsets does not matter.

Function Signature

subsets(array)

Parameters

  • array — an array of values.

Output

Return an array of arrays, where each inner array is a subset of the input.

Constraints

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

Examples

subsets([])        → [[]]
subsets([1])       → [[], [1]]
subsets([1, 2])    → [[], [1], [2], [1, 2]]
subsets(['a', 'b', 'c'])
→ [[], ['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]

Edge Cases

  • An empty array should return [[]] (an array containing only the empty subset).