Permutations II - Problem

You're given a collection of numbers that may contain duplicates, and your task is to find all possible unique permutations. Unlike the classic permutation problem, here you need to handle duplicate elements carefully to avoid generating identical permutations.

A permutation is a rearrangement of elements where order matters. For example, [1,2] and [2,1] are different permutations. However, when we have duplicates like [1,1,2], we need to ensure we don't generate the same arrangement multiple times.

Goal: Return all unique permutations in any order.

Example: Given [1,1,2], the unique permutations are [[1,1,2], [1,2,1], [2,1,1]] - notice we don't have duplicate arrangements.

Input & Output

example_1.py โ€” Basic case with duplicates
$ Input: [1,1,2]
โ€บ Output: [[1,1,2], [1,2,1], [2,1,1]]
๐Ÿ’ก Note: The array [1,1,2] has duplicates. We generate all unique permutations by ensuring that identical elements (the two 1's) are used in a consistent order to avoid duplicates.
example_2.py โ€” All elements the same
$ Input: [1,2,1,1]
โ€บ Output: [[1,1,1,2], [1,1,2,1], [1,2,1,1], [2,1,1,1]]
๐Ÿ’ก Note: With three 1's and one 2, we get exactly 4 unique permutations. The key is positioning the single 2 in different locations among the 1's.
example_3.py โ€” Single element
$ Input: [1]
โ€บ Output: [[1]]
๐Ÿ’ก Note: Edge case: A single element array has only one permutation - itself.

Visualization

Tap to expand
Smart Permutation: Only Generate What We NeedInput: [1,1,2] โ†’ Sort โ†’ [1,1,2] โ†’ Generate: [[1,1,2], [1,2,1], [2,1,1]]Decision Tree[ ][1][2][1,1][1,2][2,1][1,1,2][1,2,1][2,1,1]Duplicate Skipping LogicโŒ Skip Condition:if (i > 0 && nums[i] == nums[i-1] && !used[i-1]) continue; // Skip this duplicateโœ… Why This Works:1. We sort first: [1โ‚, 1โ‚‚, 2]2. We force order: always use 1โ‚ before 1โ‚‚3. This prevents: [1โ‚‚,1โ‚,2] vs [1โ‚,1โ‚‚,2]4. Result: Only unique arrangements!Example: At position 0, can use 1โ‚At position 1, can use 1โ‚‚ (since 1โ‚ used) or 2
Understanding the Visualization
1
Sort Input
Group identical elements together: [1,2,1] becomes [1,1,2]
2
Backtrack Smart
For each position, try unused elements, but skip duplicates intelligently
3
Skip Rule
If nums[i] == nums[i-1] and used[i-1] == false, skip nums[i]
4
Generate Unique
This ensures each unique permutation is generated exactly once
Key Takeaway
๐ŸŽฏ Key Insight: Sort first, then use the rule 'skip duplicate if previous duplicate unused' to ensure each unique permutation is generated exactly once without wasteful computation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n! ร— n)

Generate n! permutations, each taking O(n) time to create and compare

n
2n
โš  Quadratic Growth
Space Complexity
O(n! ร— n)

Store all permutations in a set, each permutation takes O(n) space

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 8
  • -10 โ‰ค nums[i] โ‰ค 10
  • The input array may contain duplicates
Asked in
Microsoft 45 Amazon 38 Google 32 Meta 28
125.4K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen