Every Unique Ordering When Values May Repeat
Implement uniquePermutationsOf
Given an array of integers that may contain repeats, produce every distinct ordering of its elements — not every ordering of its positions, since two positions holding the same value shouldn't be treated as producing two different answers.
Generating every raw arrangement and filtering afterward works: sort each row into a canonical form and collapse consecutive duplicates once everything is grouped together. But it always pays for the full n! generation first. Sorting the input up front and checking, at the moment a value is about to be tried, whether its identical predecessor is still idle or already part of the path being built, prevents a duplicate ordering from ever being generated in the first place — no cleanup pass required.
Example 1:
Input: nums = [3,3,5]
Output: [[3,3,5],[3,5,3],[5,3,3]]
Example 2:
Input: nums = [7,7]
Output: [[7,7]]
Example 3:
Input: nums = [2]
Output: [[2]]
+ 5 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 7 - ●
-10 ≤ nums[i] ≤ 10, and values may repeat - ●
Results are returned sorted in ascending (lexicographic) order for a stable, checkable answer
nums =
[3, 3, 5]