Skip to content

Instantly share code, notes, and snippets.

@southerton81
Created March 18, 2018 20:49
Show Gist options
  • Save southerton81/62a62baf4036882a84ef388dd4309ca9 to your computer and use it in GitHub Desktop.
Save southerton81/62a62baf4036882a84ef388dd4309ca9 to your computer and use it in GitHub Desktop.
Returns all possible subsets (without duplicate subsets)
let list = [];
let nums = [1,2,3];
function backtrack(nums, temparray, startindex) {
list.push(temparray.slice());
for (var i = startindex; i < nums.length; i++) {
temparray.push(nums[i]);
backtrack(nums, temparray, i + 1);
temparray.pop();
}
};
backtrack(nums, [], 0);
console.log(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment