Subsets

Medium#78subsets

You get a list of different numbers. Return every subset of it: every group you can pick, from none of the numbers to all of them, each group once, in any order. Our example: [2, 5, 8] has 2³ = 8 subsets, [ ] and [2] through [2, 5, 8].

input
nums: different integers.
returns
Every subset of nums: every group of its numbers you can pick, from none of them to all of them, each group once. Subsets in any order, numbers in a subset too.
constraints
  • 1 ≤ nums.length ≤ 10
  • −10 ≤ nums[i] ≤ 10
  • the numbers of nums are all different
our 10 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
function subsets(nums) {

}
Ctrl ↵
format
reset

saved in this browser

10 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more backtracking problems

Pattern “Backtracking” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.