Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

99. Permutations II

MediumDay 28 of 60Recursion & Backtracking

Given an array nums that may contain duplicates, return all distinct permutations of it. Two orderings that look the same must appear only once. You may return them in any order.

Example 1
Input: nums = [1,1,2]
Output: [[1,1,2],[1,2,1],[2,1,1]]
There are 3! = 6 orderings, but swapping the two 1s gives the same list, so only 3 are distinct.
Example 2
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Constraints
  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10
Hints
Frequently asked at: Amazon, Microsoft, LinkedIn
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min