✅Fully explained C++ solution with Example & Time, Space Complexity analysis!
If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistakes please let me know. Thank you!\u2
> **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistakes please let me know. Thank you!\u2764\uFE0F**\n>  \n\n---\n\n## Explanation:\n\n### Solution 01\n\n- Using ***Recursion & Backtracking.***\n- Here we\u2019ll first sort the array and for every index, we\u2019ll try to push elements in such a way that it doesn\u2019t contain any duplicates.\n- In every function call, we\u2019ll push our temp to ans vector. For the first call, it will push an empty subset.\n- Now we will iterate from index to n-1 & try to pick & not pick an element.\n- To avoid duplicates if we have already picked an element for the current index of temp with the same value as the index of the given array then we\u2019ll continue.\n- After every call we\u2019ll pop our current element.\n- Let\'s explain this with an example:\n    \n    ```\n    if the array is [1,2,2]\n    \n    help(0,num)\n    -> ans=[[]]\n    -> in for loop: \n    \t --> i=0, temp=[1], call help(1,num)\n    \t\t\t\thelp(1,num)\n    \t\t\t\t-> ans=[[],[1]]\n    \t\t\t\t-> in for loop:\n    \t\t\t\t\t --> i=1, temp=[1,2], call help(2,num)\n    \t\t\t\t\t\t\t\thelp(2,num)\n    \t\t\t\t\t\t\t\t-> ans=[[],[1],[1,2]]\n    \t\t\t\t\t\t\t\t-> in for loop:\n    \t\t\t\t\t\t\t\t\t --> i=2, temp=[1,2,2] & call help(3,num)\n    \t\t\t\t\t\t\t\t\t\t\thelp(3,num)\n    \t\t\t\t\t\t\t\t\t\t\t-> ans=[[],[1],[1,2],[1,2,3]]\n    \t\t\t\t\t\t\t\t\t\t\t-> in for loop:\n    \t\t\t\t\t\t\t\t\t\t\t\t --> i=3, loop will terminate\n    \t\t\t\t\t\t\t\t\t\t\tNow it will go back to help(2,num) & pop last element first...  \n    \t\t\t\t\t\t\t\t\t --> i=3, temp=[1,2], loop will terminate\n    \t\t\t\t\t\t\t\tNow it will go back to help(1,num) & pop the last element first...\n    \t\t\t\t\t --> i=2, temp=[1], now it will check the condition **if(i!=index && nums[i]==nums[i-1]),** here we already used 2 and our 2nd index element, so if we take 2 again it will give us duplicates. So we\'ll simply skip this.\n    \t\t\t\t\t --> i=3, temp=[], loop will terminate\n    \t\t\t\tNow it will go back to help(0,num) & pop the last element first...\n    \t --> i=1, temp=[2] & call help(2,num)\n    \t\t\t\thelp(2,num)\n    \t\t\t\t-> ans=[[],[1],[1,2],[1,2,3],[2]]\n    \t\t\t\t-> in for loop:\n    \t\t\t\t\t --> i=2, temp=[2,2], call help(3,num)\n    \t\t\t\t\t\t\t\thelp(3,num)\n    \t\t\t\t\t\t\t\t-> ans=[[],[1],[1,2],[1,2,3],[2],[2,2]]\n    \t\t\t\t\t\t\t\t-> in for loop:\n    \t\t\t\t\t\t\t\t\t\t--> i=3, temp=[2,2], loop will terminate\n    \t\t\t\t\t\t\t\tNow it will go back to help(2,num) & pop last element first...\n    \t\t\t\t\t\t--> i=3, temp=[2], loop will terminate\n    \t\t\t\tNow it will go back to help(0,num) & pop last element first...\n    \t --> i=2, temp=[], again if condition satisfied and will skip this iteration.\n    \t --> i=3, temp=[], loop will terminate\n    ```\n    \n- **Time complexity:** O(2^n * n).\n- **Space complexity:** O(2^n * k).\n\n---\n\n## Code:\n\n```cpp\n//Solution 01:\nclass Solution {\npublic:\n    vector<int> temp;\n    vector<vector<int>> ans;\n        \n    vector<vector<int>> subsetsWithDup(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        help(0, nums);\n        return ans;\n    }\n    \n    void help(int index, vector<int>& nums){\n        ans.push_back(temp);\n        \n        for(int i=index; i<nums.size(); i++){\n            if(i!=index && nums[i]==nums[i-1]) continue;\n            temp.push_back(nums[i]);\n            help(i+1, nums);\n            temp.pop_back();\n        }\n    }\n};\n```\n\n---\n\n> **Please upvote this solution**\n>
['Backtracking', 'Depth-First Search', 'Recursion', 'C', 'C++']