given a collection of integers that might contain duplicates nums return all possible subsets note the solution set must not contain duplicate subsets for example if nums 1 2 2 a solution is 2 1 1 2 2 2 2 1 2 take don t take take don t take
def subsets_unique(nums):

    def backtrack(res, nums, stack, pos):
        if pos == len(nums):
            res.add(tuple(stack))
        else:
            
            stack.append(nums[pos])
            backtrack(res, nums, stack, pos+1)
            stack.pop()

            
            backtrack(res, nums, stack, pos+1)

    res = set()
    backtrack(res, nums, [], 0)
    return list(res)