Permutations II
Problem Statement
You get a list nums that can contain repeated numbers. Return every distinct ordering of those numbers. Two orderings that look the same are listed only once.
The orderings can come back in any sequence.
A list nums of integers. It may have repeated values and has between 1 and 8 numbers.
A list of all distinct orderings. Each ordering holds every number once, and no ordering appears twice.
The two 1s look alike. Swapping them gives the same ordering, so the 3! = 6 orderings collapse into 3 distinct ones.
No value repeats, so all 3! = 6 orderings are different.
- • 1 ≤ nums.length ≤ 8
- • −10 ≤ nums[i] ≤ 10
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Generate every index order, insert each ordering into a set, and let the set drop repeats
Sort; at each slot skip a value if its equal left neighbour is unused
Run the plain permutation search over the indices, with no special rule. Put each finished ordering into a set<vector<int>>. The set removes the repeats, but only after every duplicate has already been built.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.