Given a set of positive integers, find all its subsets. The set can contain duplicate elements, so any repeated subset should be considered only once in the output.

**Examples:**

Input:S = {1, 2, 2}Output:{}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2}Explanation:The total subsets of given set are - {}, {1}, {2}, {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2} Here {2} and {1, 2} are repeated twice so they are considered only once in the output

**Prerequisite: **Power Set

The idea is to use a bit-mask pattern to generate all the combinations as discussed in previous post. But previous post will print duplicate subsets if the elements are repeated in the given set. To handle duplicate elements, we construct a string out of given subset such that subsets having similar elements will result in same string. We maintain a list of such unique strings and finally we decode all such string to print its individual elements.

Below is its implementation –

`// C++ program to find all subsets of given set. Any ` `// repeated subset is considered only once in the output ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Utility function to split the string using a delim. Refer - ` `vector<string> split(` `const` `string &s, ` `char` `delim) `
`{ ` ` ` `vector<string> elems; `
` ` `stringstream ss(s); `
` ` `string item; `
` ` `while` `(getline(ss, item, delim)) `
` ` `elems.push_back(item); `
` ` ` ` `return` `elems; `
`} ` ` ` `// Function to find all subsets of given set. Any repeated ` `// subset is considered only once in the output ` `int` `printPowerSet(` `int` `arr[], ` `int` `n) `
`{ ` ` ` `vector<string> list; `
` ` ` ` `/* Run counter i from 000..0 to 111..1*/`
` ` `for` `(` `int` `i = 0; i < (` `int` `) ` `pow` `(2, n); i++) `
` ` `{ `
` ` `string subset = ` `""` `; `
` ` ` ` `// consider each element in the set `
` ` `for` `(` `int` `j = 0; j < n; j++) `
` ` `{ `
` ` `// Check if jth bit in the i is set. If the bit `
` ` `// is set, we consider jth element from set `
` ` `if` `((i & (1 << j)) != 0) `
` ` `subset += to_string(arr[j]) + ` `"|"` `; `
` ` `} `
` ` ` ` `// if subset is encountered for the first time `
` ` `// If we use set<string>, we can directly insert `
` ` `if` `(find(list.begin(), list.end(), subset) == list.end()) `
` ` `list.push_back(subset); `
` ` `} `
` ` ` ` `// consider every subset `
` ` `for` `(string subset : list) `
` ` `{ `
` ` `// split the subset and print its elements `
` ` `vector<string> arr = split(subset, ` `'|'` `); `
` ` `for` `(string str: arr) `
` ` `cout << str << ` `" "` `; `
` ` `cout << endl; `
` ` `} `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `arr[] = { 10, 12, 12 }; `
` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); `
` ` ` ` `printPowerSet(arr, n); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python3 program to find all subsets of ` `# given set. Any repeated subset is ` `# considered only once in the output ` `def` `printPowerSet(arr, n): `
` ` ` ` `# Function to find all subsets of given set. `
` ` `# Any repeated subset is considered only `
` ` `# once in the output `
` ` `_list ` `=` `[] `
` ` ` ` `# Run counter i from 000..0 to 111..1 `
` ` `for` `i ` `in` `range` `(` `2` `*` `*` `n): `
` ` `subset ` `=` `"" `
` ` ` ` `# consider each element in the set `
` ` `for` `j ` `in` `range` `(n): `
` ` ` ` `# Check if jth bit in the i is set. `
` ` `# If the bit is set, we consider `
` ` `# jth element from set `
` ` `if` `(i & (` `1` `<< j)) !` `=` `0` `: `
` ` `subset ` `+` `=` `str` `(arr[j]) ` `+` `"|"`
` ` ` ` `# if subset is encountered for the first time `
` ` `# If we use set<string>, we can directly insert `
` ` `if` `subset ` `not` `in` `_list ` `and` `len` `(subset) > ` `0` `: `
` ` `_list.append(subset) `
` ` ` ` `# consider every subset `
` ` `for` `subset ` `in` `_list: `
` ` ` ` `# split the subset and print its elements `
` ` `arr ` `=` `subset.split(` `'|'` `) `
` ` `for` `string ` `in` `arr: `
` ` `print` `(string, end ` `=` `" "` `) `
` ` `print` `() `
` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` `arr ` `=` `[` `10` `, ` `12` `, ` `12` `] `
` ` `n ` `=` `len` `(arr) `
` ` `printPowerSet(arr, n) `
` ` `# This code is contributed by vibhu4agarwal ` |

*chevron_right*

*filter_none*

**Output:**

10 12 10 12 12 12 10 12 12

This article is contributed by **Aditya Goel**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.GeeksforGeeks.org or mail your article to contribute@GeeksforGeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Backtracking to find all subsets
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Find number of ways to form sets from N distinct things with no set of size A or B
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Partition of a set into K subsets with equal sum
- Number of subsets with sum divisible by M | Set 2
- Print all subsets of given size of a set
- Count of subsets with sum equal to X
- Number of subsets with same AND, OR and XOR values in an Array
- Python program to get all subsets of given size of a set
- Sum of product of all subsets formed by only divisors of N
- Total number of Subsets of size at most K
- Perfect Sum Problem (Print all subsets with given sum)
- Count of subsets of integers from 1 to N having no adjacent elements
- Total number of subsets in which the product of the elements is even
- Count number of subsets whose median is also present in the same subset
- Partition a set into two subsets such that the difference of subset sums is minimum
- Count non-adjacent subsets from numbers arranged in Circular fashion