Data Structures & Algorithms

Sets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def subsetsWithDup(nums: list[int]) -> list[list[int]]:
	"""
	Return all possible subsets without duplicates 
	
	Input: nums = [1,2,1]

	Output: [[],[1],[1,2],[1,1],[1,2,1],[2]]
	
	Input: nums = [7,7]
	
	Output: [[],[7], [7,7]]
	"""
	nums.sort()
	subs = { () }
	for num in nums:
		news = set()
		for sub in subs:
			news.add(sub + (num,))
		subs = subs.union(news)

	return [list(s) for s in subs]

🎰