Subsets

medium

You are given an array of integers nums in which every element is unique. Return every possible subset of nums — the power set.

A subset is any selection of elements from the array: the empty selection counts, and so does the whole array. Your answer must not contain duplicate subsets, and you may return the subsets — and the elements inside each subset — in any order.

Hints

How many subsets does an array of n elements have? Count them by hand for n = 1, 2, 3 and look for the pattern.
For each element you make one independent decision: is it in the subset or not? One complete run of decisions produces exactly one subset.
Recurse with an index i: branch once by skipping nums[i] and once by picking it. When i reaches the end, the choices made along the way form one subset — record a copy of it.

Common doubts

Each of the n elements is independently in or out — two choices per element, so 2 × 2 × … × 2 = 2^n combinations. The empty set (all out) and the full array (all in) are both included.
Yes — choosing nothing is a valid selection, and the power set always contains it. Both worked examples include [] in the answer.
Because path is a single shared list that keeps mutating as the recursion unwinds. If you store a reference to it, every saved subset later points at the same, finally-empty list. Snapshot with path[:] (Python), a spread (JavaScript), or an explicit copy (Go).
No — the judge accepts any order, both across subsets and within each subset. Different approaches naturally emit different orders, and all are correct.

Interview follow-ups

That is the Subsets II variant: sort the array first, then in the pick-or-skip recursion, skip an element when it equals the previous element and the previous one was skipped at the same level — this prevents duplicate subsets without a set-based dedup.
That is Combinations: same recursion, but prune — record when the path reaches length k, and stop descending when even taking every remaining element cannot reach k.
Yes, two ways: the cascading build (start from the empty subset and double the collection per element) or bit masks (loop the integers 0 to 2^n − 1 and read each one as an in/out vector).

Fun facts

  • The set of all subsets is called the power set, written 2^S — the notation literally comes from its size, 2^|S|.
  • The pick-or-skip decision tree here is the skeleton of nearly every backtracking problem: Combination Sum, Combinations, Subsets II, and subset-sum all reuse it with one extra rule bolted on.
  • Encoding a subset as the bits of an integer is the foundation of bitmask dynamic programming — the standard technique behind the classic traveling-salesman DP.

Asked at

AmazonGoogleMetaMicrosoftBloombergAdobe
Frequently Sometimes Occasionally
Example 1
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Every combination of the three elements appears exactly once — 2^3 = 8 subsets, from the empty set to the full array.
Example 2
Input: nums = [0]
Output: [[],[0]]
One element gives 2^1 = 2 subsets: leave it out, or take it.
Constraints

- 1 <= nums.length <= 10 - -10 <= nums[i] <= 10 - All elements of nums are unique.

Solve this problem →