The core trick is sorting first and then skipping a number at the current recursion level if it's identical to the previous one and that previous one wasn't used.
Use backtracking with sorting and a used array to generate permutations while skipping duplicates at the same recursion level. Alternatively, use a frequency map to build unique permutations by choosing each distinct number once per position. Discuss time complexity O(n * n!) and space O(n) for recursion.
Pro tip: Mention that sorting and skipping duplicates is a common pattern for combination/permutation problems, and that using a frequency map can be more efficient when there are many duplicates. Also, note that the output size is n! in the worst case, so the algorithm is optimal in terms of output size.
Confirm that the input list may contain duplicates and that we need unique permutations. Sort the list to bring duplicates together, which simplifies duplicate skipping.
Decide between using a used array with sorting or a frequency map. Explain the trade-offs: used array is simpler but requires sorting; frequency map avoids sorting and can be more efficient with many duplicates.
Recursively build permutations by choosing an unused element at each step. If using used array, skip duplicates by checking if the previous identical element is unused. If using frequency map, iterate over distinct keys and decrement counts.
State that time complexity is O(n * n!) in the worst case (all unique) and space is O(n) for recursion and used array. Mention that the number of unique permutations can be less with duplicates.
Walk through a small example with duplicates, e.g., [1,1,2], to verify correctness. Discuss potential optimizations like pruning or using iterative approaches if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.