🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\nC++ []\nclass Solution {\npublic:\n    void solve(vector<int>& nums, int i, vector<int> ds, set<vector<int>>& st) {\n        if (i == nums.size
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/3606685f-9931-4f20-aa62-79621b8c1a4f_1716257514.2342136.jpeg)\n![Screenshot 2024-05-21 074101.png](https://assets.leetcode.com/users/images/784f3f9d-0120-4011-85c9-1d1d1b3298b8_1716257523.5321796.png)\n![Screenshot 2024-05-21 074113.png](https://assets.leetcode.com/users/images/cdcfd302-53c2-468a-9423-2c546ba057fb_1716257530.7034729.png)\n```C++ []\nclass Solution {\npublic:\n    void solve(vector<int>& nums, int i, vector<int> ds, set<vector<int>>& st) {\n        if (i == nums.size()) {\n            sort(ds.begin(), ds.end());\n            st.insert(ds);\n            return;\n        }\n\n        ds.push_back(nums[i]);\n        solve(nums, i + 1, ds, st);\n        ds.pop_back();\n        solve(nums, i + 1, ds, st);\n    }\n\n    vector<vector<int>> subsetsWithDup(vector<int>& nums) {\n        vector<vector<int>> ans;\n        set<vector<int>> st;\n        vector<int> ds;\n\n        solve(nums, 0, ds, st);\n        for (auto& v : st)\n            ans.push_back(v);\n        return ans;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def subsetsWithDup(self, nums):\n        def f(index, t):\n            ans.append(list(t))\n\n            for i in range(index, len(nums)):\n                if i != index and nums[i] == nums[i - 1]:\n                    continue\n                t.append(nums[i])\n                f(i + 1, t)\n                t.pop()\n\n        ans = []\n        nums.sort()\n        f(0, [])\n        return ans\n```\n```Java []\n\nclass Solution {\n    private void f(int index, int[] nums, List<Integer> t, List<List<Integer>> ans) {\n        ans.add(new ArrayList<>(t));\n\n        for (int i = index; i < nums.length; i++) {\n            if (i != index && nums[i] == nums[i - 1])\n                continue;\n            t.add(nums[i]);\n            f(i + 1, nums, t, ans);\n            t.remove(t.size() - 1);\n        }\n    }\n\n    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        List<List<Integer>> ans = new ArrayList<>();\n        List<Integer> t = new ArrayList<>();\n\n        Arrays.sort(nums);\n        f(0, nums, t, ans);\n        return ans;\n    }\n}\n```\n\n```python []\nclass Solution(object):\n    def subsetsWithDup(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: List[List[int]]\n        """\n        nums = sorted(nums)\n        seen_subsets = {}\n        count = 2 ** len(nums) - 1\n        while count >= 0:\n            binary_count = str(bin(count))[2:]\n            binary_count = binary_count.zfill(len(nums))\n\n            subset = []\n            for idx, c in enumerate(binary_count):\n                if c == "1":\n                    subset.append(nums[idx])\n            if str(subset) not in seen_subsets:\n                seen_subsets[str(subset)] = 1\n            count = count - 1\n\n        subsets = list(seen_subsets.keys())\n        return [ json.loads(subset) for subset in subsets ]\n        \n```\n\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n      O(nlogn)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n      O(n)\n# Code\n```\nclass Solution {\npublic:\n    void solve(vector<int>& nums, int i, vector<int> ds, set<vector<int>>& st) {\n        if (i == nums.size()) {\n            sort(ds.begin(), ds.end());\n            st.insert(ds);\n            return;\n        }\n\n        ds.push_back(nums[i]);\n        solve(nums, i + 1, ds, st);\n        ds.pop_back();\n        solve(nums, i + 1, ds, st);\n    }\n\n    vector<vector<int>> subsetsWithDup(vector<int>& nums) {\n        vector<vector<int>> ans;\n        set<vector<int>> st;\n        vector<int> ds;\n\n        solve(nums, 0, ds, st);\n        for (auto& v : st)\n            ans.push_back(v);\n        return ans;\n    }\n};\n```
['Array', 'Dynamic Programming', 'Backtracking', 'Bit Manipulation', 'C', 'Python', 'C++', 'Java', 'Python3']