Untitled

 avatar
user_1671475754
javascript
4 years ago
699 B
7
Indexable
const subsets = (arr) => { //[1,2,3]
  const combination_of_results = [];

  const subset = [] //the subset we will get and add to combination

  const left_right_helper = (i) => {
    if(i > arr.length-1){ //i is out of bounds (ex: i + 1 = 4, 4> len of [1,2,3])
      combination_of_results.push(subset.slice());//we want to push the leaves onto the result b/c we REACHED the END
      return;
    }

    // left add case (decision to include arr[i])
    subset.push(arr[i]);
    left_right_helper(i+1);

    // right keep same case (decision to not arr[i])
    subset.pop(arr[i]);
    left_right_helper(i+1);
  }

  left_right_helper(0);
  return combination_of_results;
};
Editor is loading...