MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / powerset

Function powerset

recursion/powerset.js:21–36  ·  view source on GitHub ↗
(array, idx = null)

Source from the content-addressed store, hash-verified

19// runs in the same time/space complexity
20
21function powerset(array, idx = null) {
22 if (idx === null) {
23 idx = array.length - 1;
24 }
25 if (idx < 0) {
26 return [[]];
27 }
28 const ele = array[idx];
29 const subsets = powerset(array, idx - 1);
30 const length = subsets.length;
31 for (let i = 0; i < length; i++) {
32 const currentSubset = subsets[i];
33 subsets.push(currentSubset.concat(ele));
34 }
35 return subsets;
36 }
37
38 const array = [];
39

Callers 1

powerset.jsFile · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected