class Solution {public: vector > subsetsWithDup(vector &S) { if (S.empty()) return {}; vector > res(1); sort(S.begin(), S.end()); int size = 1, last = S[0]; for (int i = 0; i < S.size(); ++i) { if (last != S[i]) { last = S[i]; size = res.size(); } int newSize = res.size(); for (int j = newSize - size; j < newSize; ++j) { res.push_back(res[j]); res.back().push_back(S[i]); } } return res; }};
class Solution {public: vector > subsetsWithDup(vector &S) { if (S.empty()) return {}; vector > res; vector out; sort(S.begin(), S.end()); getSubsets(S, 0, out, res); return res; } void getSubsets(vector &S, int pos, vector &out, vector > &res) { res.push_back(out); for (int i = pos; i < S.size(); ++i) { out.push_back(S[i]); getSubsets(S, i + 1, out, res); out.pop_back(); while (i + 1 < S.size() && S[i] == S[i + 1]) ++i; } }};