Permutations II

Given an integer array nums that may contain duplicates, return all possible unique permutations of nums.

The returned permutations may be in any order. Each permutation must contain every element from nums exactly once, and duplicate permutations must not be included in the result.

Example 1
Inputnums = [1,1,2]
Output[[1,1,2],[1,2,1],[2,1,1]]
The two 1 values are duplicates, so only three distinct orderings are possible.
Example 2
Inputnums = [1,2,3]
Output[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
All values are distinct, so there are 3! = 6 unique permutations.

Constraints

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10

Asked at 9 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate