Loading problem...
Given a collection of integers that may include duplicate values, generate all distinct orderings of the elements. Each arrangement should use every element exactly once, and you must ensure that no two arrangements in your result are identical. The output can be returned in any order.
An arrangement (or permutation) of a collection is a specific ordering of all its elements. When the collection contains duplicate values, some arrangements that would otherwise be considered different (based on position) become identical when considering the actual values. Your task is to eliminate such duplicates and return only the unique arrangements.
For instance, given the collection [1, 1, 2], the element at index 0 and the element at index 1 are both 1. Swapping them produces the same arrangement [1, 1, 2], which should appear only once in the result.
nums = [1,1,2][[1,1,2],[1,2,1],[2,1,1]]Although there are 3! = 6 ways to arrange three elements by position, since two of the elements are identical (both are 1), only 3 unique orderings exist. The arrangements [1,1,2], [1,2,1], and [2,1,1] are the distinct orderings.
nums = [1,2,3][[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]With all distinct elements, we have 3! = 6 unique arrangements. No duplicates need to be eliminated.
nums = [1][[1]]A single-element collection has exactly one arrangement: the element itself.
Constraints