Permutation of array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A permutation of an array is any arrangement of its elements in a different order. In coding problems, the real question is usually not the definition but how to generate permutations efficiently and how to avoid duplicate output when the array contains repeated values.
Understand the Growth Rate
If an array has n distinct elements, it has n! permutations.
For example:
- '
3elements produce6permutations' - '
4elements produce24' - '
5elements produce120'
This grows very quickly, so permutation generation is inherently expensive for larger arrays. That is not a flaw in the algorithm; it is the nature of the problem.
Generate Permutations with Backtracking
A standard approach is recursive backtracking with swaps.
This works by fixing one position at a time and recursively permuting the remaining suffix.
Avoid Duplicate Permutations When Values Repeat
If the array contains repeated values, naive backtracking can emit duplicate permutations. A common fix is to sort first and skip repeated choices at each depth.
That is the right pattern when the task is “unique permutations” rather than “all index permutations.”
Use a Library When the Language Provides One
If the goal is simply to iterate permutations rather than implement the algorithm yourself, a library function is often clearer.
This is concise and reliable, but it does not change the factorial cost.
Materialize or Stream Based on the Goal
Some code wants a complete list of permutations. Other code only wants to process them one at a time. If you only need iteration, prefer a generator or streaming approach because it avoids storing all permutations in memory at once.
That distinction does not fix the time complexity, but it can greatly improve memory behavior.
Decide Whether You Need All Permutations
Many problems that mention permutations do not actually require materializing all of them. Sometimes you need:
- only the next lexicographic permutation
- the count of permutations
- one valid permutation under constraints
- the best permutation under a scoring rule
If that is the real task, generating all permutations may be unnecessary and too expensive.
Common Pitfalls
- Forgetting that permutation generation grows factorially and becomes infeasible quickly.
- Returning the same array reference repeatedly instead of copying the current state.
- Generating duplicate permutations when the input contains repeated values.
- Using recursion without understanding the backtracking undo step.
- Enumerating all permutations when the real problem needs only one or a count.
Summary
- A permutation is a reordering of array elements.
- Backtracking with swaps is a standard way to generate permutations.
- Inputs with duplicates require extra logic to avoid repeated results.
- Library helpers such as
itertools.permutationsare great when implementation details do not matter. - Always check whether you really need all permutations before generating them.
Related reading
- Permutation of string as substring of another
- Permutation of String letters How to remove repeated permutations?
- Permutations excluding repeated characters
- Permutations of a binary tree
- PHP algorithm to generate all combinations of a specific size from a single set
- PHP array delete by value (not key)
- Permutations of binary number by swapping two bits not lexicographically
- Permutations of letters and numbers in a phone number

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.