given a set of distinct integers nums return all possible subsets note the solution set must not contain duplicate subsets for example if nums 1 2 3 a solution is 1 2 1 3 1 2 3 1 2 3 2 3 param nums listint return settuple this explanation is from leetnik leetcode this is an amazing solution learnt a lot number of subsets for 1 2 3 23 why case possible outcomes for the set of subsets 1 take or dont take 2 2 take or dont take 2 3 take or dont take 2 therefore total 222 23 1 2 3 1 2 1 3 2 3 1 2 3 lets assign bits to each outcome first bit to 1 second bit to 2 and third bit to 3 take 1 dont take 0 0 0 0 0 dont take 3 dont take 2 dont take 1 1 0 0 1 dont take 3 dont take 2 take 1 1 2 0 1 0 dont take 3 take 2 dont take 1 2 3 0 1 1 dont take 3 take 2 take 1 1 2 4 1 0 0 take 3 dont take 2 dont take 1 3 5 1 0 1 take 3 dont take 2 take 1 1 3 6 1 1 0 take 3 take 2 dont take 1 2 3 7 1 1 1 take 3 take 2 take 1 1 2 3 in the above logic insert si only if ji1 true j e 0 1 2 3 4 5 6 7 i ith element in the input array element 1 is inserted only into those places where 1st bit of j is 1 if j 0 1 for above above eg this is true for sl no j 1 3 5 7 element 2 is inserted only into those places where 2nd bit of j is 1 if j 1 1 for above above eg this is true for sl no j 2 3 6 7 element 3 is inserted only into those places where 3rd bit of j is 1 if j 2 1 for above above eg this is true for sl no j 4 5 6 7 time complexity on2n for every input element loop traverses the whole solution set length i e 2n param nums list int return set tuple this explanation is from leet_nik leetcode this is an amazing solution learnt a lot number of subsets for 1 2 3 2 3 why case possible outcomes for the set of subsets 1 take or dont take 2 2 take or dont take 2 3 take or dont take 2 therefore total 2 2 2 2 3 1 2 3 1 2 1 3 2 3 1 2 3 lets assign bits to each outcome first bit to 1 second bit to 2 and third bit to 3 take 1 dont take 0 0 0 0 0 dont take 3 dont take 2 dont take 1 1 0 0 1 dont take 3 dont take 2 take 1 1 2 0 1 0 dont take 3 take 2 dont take 1 2 3 0 1 1 dont take 3 take 2 take 1 1 2 4 1 0 0 take 3 dont take 2 dont take 1 3 5 1 0 1 take 3 dont take 2 take 1 1 3 6 1 1 0 take 3 take 2 dont take 1 2 3 7 1 1 1 take 3 take 2 take 1 1 2 3 in the above logic insert s i only if j i 1 true j e 0 1 2 3 4 5 6 7 i ith element in the input array element 1 is inserted only into those places where 1st bit of j is 1 if j 0 1 for above above eg this is true for sl no j 1 3 5 7 element 2 is inserted only into those places where 2nd bit of j is 1 if j 1 1 for above above eg this is true for sl no j 2 3 6 7 element 3 is inserted only into those places where 3rd bit of j is 1 if j 2 1 for above above eg this is true for sl no j 4 5 6 7 time complexity o n 2 n for every input element loop traverses the whole solution set length i e 2 n
def subsets(nums):
    
    n = len(nums)
    total = 1 << n
    res = set()

    for i in range(total):
        subset = tuple(num for j, num in enumerate(nums) if i & 1 << j)
        res.add(subset)

    return res