You get an integer array nums, and some of its values may repeat. Return every distinct subset of it: every group you can form by keeping some of its numbers and leaving out the rest, from the empty subset [] up to the whole array.
Two subsets are the same when they hold the same values, each the same number of times. Order doesn't matter, and neither does which copy of a repeated value you kept. So with nums = [3,1,3], keeping the first 3 or the last 3 gives the same subset [3], and [1,3] and [3,1] are one subset too. List each distinct subset exactly once.
You may return the subsets in any order, and the numbers inside each subset in any order too.
A subset can hold zero, one or two 1s, and zero, one or two 6s. Three choices for each value give 3 · 3 = 9 subsets.
All three numbers are equal, so a subset is decided only by how many 4s it keeps.
With no repeats, every one of the 2² = 4 ways to keep or drop each number is a different subset.
Constraints
1 <= nums.length <= 10-10 <= nums[i] <= 10Sort nums first so equal values sit side by side. Then a subset is fully described by how many copies of each distinct value it takes.
Without repeats this is plain Subsets: each number is in or out, so n numbers give 2ⁿ subsets. Repeats break that count. In [3,1,3], keeping only the first 3 and keeping only the last 3 are two different in-or-out patterns, but they are one subset, [3]. The pattern view sees positions. The answer cares about values.
Time O(n · 2ⁿ)Space O(n · 2ⁿ)
The full walkthrough and code are part of Premium