/47. Unique Permutations of Integer Array

47. Unique Permutations of Integer Array

Medium
Arrays63.1% acceptance

Given an integer array input_array that may contain duplicate elements, return a list of all unique permutations of input_array. Each permutation should be represented as a list of integers. The order of permutations in the output does not matter.

Example 1

Input: [2,2,1]

Output: [[2,2,1],[2,1,2],[1,2,2]]

Explanation: All unique permutations of [2,2,1] are listed.

Example 2

Input: [3,3,3]

Output: [[3,3,3]]

Explanation: Only one unique permutation exists for [3,3,3].

Example 3

Input: [0,1]

Output: [[0,1],[1,0]]

Explanation: Two unique permutations for [0,1].

Constraints

  • 1 <= len(input_array) <= 8
  • -10 <= input_array[i] <= 10 for all i in input_array
Python (current runtime)

Case 1

Input: [4,4,5]

Expected: [[4,4,5],[4,5,4],[5,4,4]]

Case 2

Input: [1,2,2,3]

Expected: [[1,2,2,3],[1,2,3,2],[1,3,2,2],[2,1,2,3],[2,1,3,2],[2,2,1,3],[2,2,3,1],[2,3,1,2],[2,3,2,1],[3,1,2,2],[3,2,1,2],[3,2,2,1]]

Case 3

Input: [7]

Expected: [[7]]