# 90.subsets-ii

## Statement

• Difficulty: Medium
• Tag: `位运算` `数组` `回溯`

``````输入：nums = [1,2,2]

``````

``````输入：nums = [0]

``````

• `1 <= nums.length <= 10`
• `-10 <= nums[i] <= 10`

• Difficulty: Medium
• Tag: `Bit Manipulation` `Array` `Backtracking`

Given an integer array `nums` that may contain duplicates, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:

``````Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
``````

Example 2:

``````Input: nums = [0]
Output: [[],[0]]
``````

Constraints:

• `1 <= nums.length <= 10`
• `-10 <= nums[i] <= 10`

## Solution

``````from typing import List

class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
res = []
for S in range(1 << n):
cur_res = []
for i in range(n):
if (S >> i) & 1:
cur_res.append(nums[i])
res.append(cur_res)
uni = {}
r_res = []
for item in res:
item.sort()
key = str(item)
if key not in uni.keys():
uni[key] = 1
r_res.append(item)
return r_res
``````