All possible combinations of an array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When people ask for "all possible combinations of an array," they usually mean one of two things: all subsets of the array, or all groups of a fixed size. Those are different from permutations, where order matters.
Getting that distinction right matters because the algorithm, output size, and performance all change. Before writing code, decide whether you want the full power set, combinations of length k, or ordered arrangements.
Combination vs Permutation
For an array ["A", "B", "C"], the combinations of size 2 are:
The permutations of size 2 are:
If you want every possible combination across all lengths, you are asking for the power set. An array of length n has 2^n subsets, including the empty one.
Backtracking for Fixed-Size Combinations
Backtracking is the standard way to generate combinations of size k. The idea is simple: choose the current element or skip it, while keeping the result in sorted index order so duplicates from reordering never appear.
Output:
This algorithm visits exactly the valid combinations. For fixed k, that is usually what you want.
Generating the Full Power Set
If you need combinations of every possible size, generate the power set instead. One clean approach is iterative: start with the empty subset, then extend existing subsets with each new element.
Output:
This is concise and easy to reason about. The unavoidable cost is O(2^n) because there are 2^n subsets to return.
Using the Standard Library
If you are using Python, itertools.combinations is the best default for fixed-size combinations. It is fast, memory-efficient, and already handles the index ordering correctly.
For all sizes, combine it with a loop:
This version yields results lazily instead of building one huge list up front.
Handling Duplicate Values
Arrays sometimes contain repeated values such as [1, 1, 2]. If you generate combinations by index, you can end up with repeated value combinations that look identical in the output.
To avoid that, sort the input and skip repeated choices at the same recursion depth:
That pattern is common in interview problems and search code.
Common Pitfalls
The biggest mistake is mixing up combinations and permutations. If order does not matter, do not use an algorithm that rearranges the same elements into multiple orders.
Another common issue is forgetting the output size. Returning all subsets is exponential. That is not an implementation bug; it is inherent in the problem. If n is large, prefer a generator and process results incrementally.
Duplicates are another trap. If the input can contain repeated values, define whether combinations are based on positions or values. Those produce different answers.
Finally, do not mutate and store the same working list object without copying it. If you append current directly during backtracking, later modifications will corrupt every stored result.
Summary
- "All combinations" can mean fixed-size combinations or the full power set.
- Combinations ignore order; permutations do not.
- Backtracking is a clear approach for combinations of size
k. - The power set has
2^nsubsets, so the output size grows very quickly. - In Python,
itertools.combinationsis usually the best fixed-size solution. - If the input contains duplicates, sort first and skip repeated choices carefully.

