[Python3] Simple Recursive Solution
Intuition\nThe intuition behind the code is to use a recursive approach to generate subsets. At each step, we have two choices: either include the current eleme
# Intuition\nThe intuition behind the code is to use a recursive approach to generate subsets. At each step, we have two choices: either include the current element in the subset or exclude it. By making these choices for each element in the array, we can generate all possible subsets.\n\n# Approach\n**Sort the array nums** \nSorting the array ensures that duplicate elements will appear next to each other, making it easier to handle them later in the code.\n\n**Create the recursive function subsets** \nThis function takes two parameters: index (the current index being considered) and elements (the current subset being constructed).\n\n**Handle base case** \nIf the current index reaches the length of the array (index == len(nums)), it means we have processed all elements. At this point, we check if the elements subset is already present in the res list. If not, we append it to res. This check is necessary to avoid duplicate subsets.\n\n**Make recursive calls**\n**Not Pick**\nCall subsets recursively with the next index without picking the current element (subsets(index + 1, elements)). This represents the choice of excluding the current element from the subset.\n**Pick**\nCall subsets recursively with the next index, including the current element (subsets(index + 1, elements + [nums[index]])). This represents the choice of picking the current element and adding it to the subset.\n\n**Duplicate Check**\nThe code avoids duplicates by checking if a subset already exists in the res list before appending it. We sort the input array in the start to ensure that duplicate elements will be adjacent, simplifying the duplicate check.\n\nNow, we call the subsets function initially with index = 0 and an empty elements list.\n\nFinally, we return the final res list containing all the generated subsets.\n\n# Complexity\n- Time complexity: **O(2^n)** \n\n- Space complexity: **O(2^n)** \n\n# Code\n```\nclass Solution:\n    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:\n        res = []\n        nums.sort()\n        def subsets(index, elements):\n            # base case\n            if index == len(nums):\n                res.append(elements) if elements not in res else None\n                return\n\n            subsets(index + 1, elements) # not pick\n            subsets(index + 1, elements + [nums[index]]) # pick\n\n        subsets(0, [])\n        return res\n```
['Recursion', 'Python3']