// C++ vector<vector<int>> subsetsWithDup(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> ret = {{}}; int size = (int)nums.size(); for (auto i = 0; i < size;) { int n = nums[i]; int count = 1; while (i + count < size && nums[i+count] == n) { ++count; } int retSize = (int)ret.size(); for (auto j = 0; j < retSize; ++j) { auto subnums = ret[j]; for (auto k = 0; k < count; ++k) { subnums.push_back(n); ret.push_back(subnums); } } i += count; } return ret; }