Skip to content

Instantly share code, notes, and snippets.

@arazmj
Last active August 11, 2020 06:35
Show Gist options
  • Save arazmj/230acd921dc63bdd01b5a067d208a955 to your computer and use it in GitHub Desktop.
Save arazmj/230acd921dc63bdd01b5a067d208a955 to your computer and use it in GitHub Desktop.
All uniques subsets of a set with duplicate elements
def subsets(a):
result = [[]]
a = sorted(a)
i = 0
while i < len(a):
duplicates = 1
while i < len(a) - 1 and a[i] == a[i + 1]:
i += 1
duplicates += 1
size = len(result)
for j in range(size):
s = result[j].copy()
for k in range(duplicates):
s += [a[i]]
result += [s.copy()]
i += 1
return result
print(subsets([1, 2, 2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment