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
Visualization
Time & Space Complexity
Generate n! permutations, each taking O(n) time to create and compare
Store all permutations in a set, each permutation takes O(n) space
Constraints
- 1 โค nums.length โค 8
- -10 โค nums[i] โค 10
- The input array may contain duplicates