# Share my recursive and bit operation solutions

• ``````// bit operation: first get all results, finally delete the duplicates
vector<vector<int> > subsetsWithDup1(vector<int> &S) {
sort(S.begin(), S.end());
int len = S.size();
int n = pow(2, len);
vector<vector<int> > ans(n, vector<int>());

for (int i = 0; i < len; ++i) {
for (int j = 0; j < n; ++j) {
if (j >> i & 1) ans[j].push_back(S[i]);
}
}

sort(ans.begin(), ans.end());
ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
return ans;
}

// iterative solution: ignore the duplicates directly
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > ans(1, vector<int>());
int len = S.size();
int s = 0, e = 0;

vector<int> tmp;
for(int i = 0; i < len; ++i) {
s = i >= 1 && S[i] == S[i-1] ? e : 0;
e = ans.size();
for(int j = s; j < e; ++j) {
tmp = ans[j];
tmp.push_back(S[i]);
ans.push_back(tmp);
}
}

return ans;
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.