Skip to problem
←

Permutations II

Medium 0 attempts

Given an integer array nums, return every distinct permutation of its values. Each permutation must use all entries exactly once, preserving the number of occurrences of each value.

Repeated values are indistinguishable: swapping two equal entries does not create a new permutation. Return each distinct arrangement once, in any outer order. The order of values within each arrangement matters.

For nums = [1, 1, 2], return [1, 1, 2], [1, 2, 1], and [2, 1, 1]. If all values are equal, there is exactly one permutation. Negative values and zero follow the same rules.

Function contract

Implement permuteUnique(nums).

Output: Return all distinct full-length permutations exactly once. Outer array order does not matter; order inside each permutation does.

Example 1

Input
nums = [1,1,2]
Output
[[1,1,2],[1,2,1],[2,1,1]]

Example 2

Input
nums = [1,1]
Output
[[1,1]]
Constraints
  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10

Backtracking with Value Frequencies

Count each distinct value in a frequency map. At each position, choose any value with a positive remaining count, decrement its count, and append it to the current path. After exploring that choice, remove it and restore the count.

When the path contains as many entries as the input, save a copy. The frequency counts guarantee that every entry is used exactly once. Choosing by value rather than by original index avoids duplicate arrangements: equal entries do not create separate branches for the same position.

Every possible arrangement can be reached by choosing its values in order, so the search is complete. The input array is not modified.

javascript
function permuteUnique(nums) {
  const result = [];
  const freq = new Map();
  for (const n of nums) freq.set(n, (freq.get(n) || 0) + 1);

  function backtrack(path) {
    if (path.length === nums.length) {
      result.push([...path]);
      return;
    }
    for (const [num, count] of freq) {
      if (count === 0) continue;
      freq.set(num, count - 1);
      path.push(num);
      backtrack(path);
      path.pop();
      freq.set(num, count);
    }
  }

  backtrack([]);
  return result;
}

Time: O(n! * n) — at most n! permutations, each taking O(n) to copy. Space: O(n) auxiliary space for the working path, recursion, and frequency map, plus O(P × n) for P returned permutations. If value frequencies are c₁, c₂, …, then P = n! / (c₁! × c₂! × …).

Loading discussion…

Saved in this browser only. Private to you.

JavaScript