Simple iterative solution
If we want to insert an element which is a dup, we can only insert it after the newly inserted elements from last step.\n\n    vector > subsetsWithDup(vector &S
If we want to insert an element which is a dup, we can only insert it after the newly inserted elements from last step.\n\n    vector<vector<int> > subsetsWithDup(vector<int> &S) {\n        sort(S.begin(), S.end());\n        vector<vector<int>> ret = {{}};\n        int size = 0, startIndex = 0;\n        for (int i = 0; i < S.size(); i++) {\n            startIndex = i >= 1 && S[i] == S[i - 1] ? size : 0;\n            size = ret.size();\n            for (int j = startIndex; j < size; j++) {\n                vector<int> temp = ret[j];\n                temp.push_back(S[i]);\n                ret.push_back(temp);\n            }\n        }\n        return ret;\n    }
[]