Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 791 Bytes

by-the-power-set-of-castle-grayskull.md

File metadata and controls

34 lines (26 loc) · 791 Bytes

By the Power Set of Castle Grayskull 5 Kyu

LINK TO THE KATA - MATHEMATICS ALGORITHMS

Description

Write a function that returns all of the sublists of a list/array.

Example:

power([1,2,3]);=>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

For more details regarding this, see the power set entry in wikipedia.

Solution

const power = array => {
  let results = [[]]
  for (let i = 0; i < array.length; i++) {
    let len = results.length
    for (let j = 0; j < len; j++) {
      results.push([...results[j], array[i]])
    }
  }
  return results
}