Unique Permutations: Skip Duplicate Choices at One Depth
By Hourly DSA · · 4 min read
Generate unique permutations by choosing each distinct available value once at each position in the result. Equal values can still appear together in a permutation. What you skip is a repeated branch that would produce the same remaining answers.
For [1, 1, 2], the unique results are [1, 1, 2], [1, 2, 1], and [2, 1, 1]. Treating the two ones as different output values would generate duplicates.
Separate positions from values
Imagine labeling the input positions as 1a, 1b, and 2. A permutation may use both ones because they come from different positions. A used-position array prevents taking the same position twice within one path.
But paths beginning with 1a and 1b produce identical value sequences after the labels are removed. A used-position array alone does not prevent that duplicated work.
You need a second rule: within one recursive call, try each available value only once as the next choice.
Give each call its own set
Maintain a shared path and used-position flags. Create a fresh tried set inside every recursive call:
search():
if length(path) == length(values):
append a copy of path to results
return
tried = empty set
for i from 0 through length(values) - 1:
if used[i] or values[i] is in tried:
continue
add values[i] to tried
used[i] = true
append values[i] to path
search()
remove last element from path
used[i] = false
Begin with an empty path, an empty results list, and all used flags false. This version does not require sorting the input.
The tried set belongs to one choice position under one particular prefix. It must not be global.
Trace the two ones
At the root, choose 1a. The next recursive call has a fresh set, so it can choose 1b, producing the prefix [1, 1]. Choosing 2 completes [1, 1, 2].
Back at prefix [1], choosing 2 next produces [1, 2, 1].
When the root later considers 1b, its own set already contains the value 1. It skips that branch because the first choice of 1 has already explored all distinct continuations beginning with 1.
The root can still choose 2, which produces [2, 1, 1].
Why is skipping an equal choice safe?
At a fixed prefix, choosing either of two unused equal values leaves the same multiset of values available. Their position labels differ, but those labels are absent from the requested output.
One of those choices therefore covers all distinct value sequences the other could produce. This argument would fail if the output distinguished items by identity, even when their displayed values matched.
That distinction also matters when choosing between backtracking and dynamic programming: define exactly what counts as a different answer.
Restore the path, preserve the result
After returning from a branch, remove its last value and clear its used flag. Otherwise, later branches inherit a choice that should already have been undone.
When saving a complete result, copy the path. Saving a reference to the mutable working list can make earlier answers change as the search continues.
Don't remove the chosen value from this call's tried set after returning. That set remembers which sibling branches have already been explored.
Include the output in the cost
For n distinct values, there are n! results, and copying each takes O(n), giving O(n × n!) time for this implementation under expected constant-time set operations. Repeated values reduce the number of unique results to n! / (c1! × c2! × ...), where each c is a value's frequency.
The path and used flags occupy O(n) space. This particular implementation can retain O(n²) set entries across active recursive calls, excluding stored output. A sorted implementation with a duplicate-skip condition can avoid those per-call sets.
Test the scope of the set
For [4, 4], should choosing the first 4 prevent the next call from choosing the second?
No. The answer [4, 4] needs both positions. Only sibling choices share a tried set.
Use Hourly DSA to practice identifying which state belongs to a whole search, one path, or one call.
Read next
Review what a set remembers and when a map needs to store additional information.