> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/power-set.md).

# Power Set

![](/files/h0SsSBFay14L2c9aQUs7)

* n x 2^n, n x 2^n

```jsx
function powerset(arr) {
  let answer = [[]];
  
  for (const n of arr) {
    const temp = [];
    for (const el of answer) {
      const next = [...el, n];
      temp.push(next);
    }
    answer.push(...temp);
  }
  
  return answer;
}

// Do not edit the line below.
exports.powerset = powerset;
```

* simpler

```jsx
function powerset(arr) {
  let answer = [[]];
  
  for (const n of arr) {
	const answerLenBeforeThisIter = answer.length;
    for (let i = 0; i < answerLenBeforeThisIter; i++) {
      answer.push(answer[i].concat(n));
    }
  }
  
  return answer;
}

// Do not edit the line below.
exports.powerset = powerset;
```
