C++ | Well Commented | Backtracking | Faster than 100% C++ submissions
Complexity\n- Time complexity: O(2^N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(2^N)\n Add your space complexity here, e.g. O(n) \n\n
# Complexity\n- Time complexity: $$O(2^N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(2^N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\nprivate:\n    void backTrack(vector<vector<int>>& ans, vector<int>& nums, vector<int>& curr, int index) {\n        ans.push_back(curr);  // Add current subset to the answer vector\n\n        // Generate subsets by including elements starting from the given index\n        for (int i = index; i < nums.size(); i++) {\n            // Skip duplicate elements to avoid generating duplicate subsets\n            if (i != index && nums[i] == nums[i - 1])\n                continue;\n\n            curr.push_back(nums[i]);  // Include the current element in the current subset\n            backTrack(ans, nums, curr, i + 1);\n            curr.pop_back();  // Remove the current element to backtrack\n        }\n    }\n\npublic:\n    vector<vector<int>> subsetsWithDup(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        vector<vector<int>> ans;\n        vector<int> curr;  // Vector to store the current subset\n\n        backTrack(ans, nums, curr, 0);\n        return ans\n    }\n};\n\n```
['Backtracking', 'Recursion', 'C++']