The backtracking part wasn't the hard bit.
Start by sorting the array to group duplicates, then use backtracking with a used array to build permutations. At each recursion level, skip a duplicate value if its previous identical value hasn't been used in the current path, ensuring each unique permutation is generated exactly once. Finally, analyze time and space complexity, noting the worst-case O(n * n!) time and O(n) space for recursion and used array.
Pro tip: Explicitly connect the duplicate-skipping logic to the sorted order and the used array—this shows you understand why the condition `used[i-1] == false` prevents duplicates without needing a set. Also, mention that the same deduplication pattern applies to combination sum and subset problems, demonstrating pattern recognition.
Confirm that the output should be unique permutations and that order doesn't matter. Sort the input array to bring duplicates together, which simplifies duplicate skipping.
Use a recursive function that builds a permutation path. Maintain a boolean `used` array to track which indices are already in the current path. At each step, iterate over all indices, skip used ones, and skip duplicates when the previous identical value is not used.
Inside the loop, if `i > 0` and `nums[i] == nums[i-1]` and `!used[i-1]`, continue. This ensures that for a group of equal values, they are used in a fixed order, preventing duplicate permutations.
Explain that sorting groups duplicates, and the skip condition ensures that among equal elements, only the leftmost unused one can be chosen next. This enforces a canonical order for identical values, so each unique permutation is generated exactly once.
Time: O(n * n!) in the worst case (all distinct), but with duplicates it's O(n * U) where U is the number of unique permutations. Space: O(n) for recursion stack and used array, plus O(n) for the path, excluding output storage.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.