DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on • Edited on

Unique-Subset-Array

var subsets = function (nums) {
  let index = 0;
  let subset = [];
  let tempArray = [];

  findSubset(index, nums, subset, tempArray);
  return subset;
};

const findSubset = (index, nums, subset, tempArray) => {
  if (index >= nums.length) {
    subset.push([...tempArray]);
    return;
  }

  findSubset(index + 1, nums, subset, tempArray);
  tempArray.push(nums[index]);
  findSubset(index + 1, nums, subset, tempArray);
  tempArray.pop();
};

console.log(subsets([1, 2, 3]));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)