Skip to main content

Permutations II

Question

What is the most efficient algorithm for finding all distinct permutations of a list of numbers with duplicates?

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

Solution

all//Permutations II.py


from itertools import permutations

# Get all permutations of [1, 2, 3]
perm = permutations([1, 2, 3], 3)

# Print the obtained permutations
for i in list(perm):
print(i)