PHP Find All somewhat Unique Combinations of an 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
Generating combinations means selecting items without caring about order. In other words, [1, 2] and [2, 1] represent the same combination. In PHP, the cleanest solution is usually a recursive function that moves forward through the array so it never revisits earlier positions.
Fixed-Length Combinations
A standard recursive approach builds one partial combination at a time and advances the starting index after each choice. That prevents permutations of the same values from being produced.
The output contains each pair once:
The key idea is that after choosing index i, the recursion continues from i + 1, never from the beginning.
Generating All Combination Lengths
Sometimes you want every non-empty combination, not just one length. Build that by calling the fixed-length function repeatedly.
That returns single-item, two-item, and three-item combinations while still ignoring order.
Handling Duplicate Values in the Input
The phrase “somewhat unique” often means the input array itself may contain duplicates. In that case, the basic recursion avoids permutation duplicates, but it can still emit repeated value combinations because identical elements at different indices are treated as separate choices.
For example, input [1, 1, 2] can produce duplicate [1, 2] combinations unless you deduplicate deliberately.
A simple method is to sort the array and skip repeated values at the same recursion depth.
Sorting once outside the recursion would be more efficient, but this version keeps the main idea visible.
Complexity and Practical Limits
Combination generation grows quickly. For an array of length n, the number of k-combinations is “n choose k”. That means output size becomes the main cost. If you ask for all combinations of a 20-element array, the result can be very large regardless of implementation details.
When result size matters, consider streaming combinations one at a time with a generator instead of storing them all in memory.
Common Pitfalls
A common mistake is using nested loops tailored to one specific length, such as pairs or triplets. That works only for fixed sizes and becomes unmaintainable fast.
Another issue is confusing combinations with permutations. If order should not matter, the recursion must move forward through the array instead of restarting from index 0.
When the input contains duplicate values, developers often think their algorithm is wrong because repeated combinations appear. The real problem is that duplicate indices still count as distinct choices unless you skip equal values deliberately.
Summary
- Use recursive forward indexing to generate combinations without permutation duplicates.
- Build fixed-length combinations first, then compose all lengths if needed.
- Duplicate values in the input require extra deduplication logic.
- Combination counts grow quickly, so output size becomes the main cost.
- For large inputs, consider generators instead of storing every result at once.
Related reading
- PHP How to sort values of an array in alphabetical order?
- PHP RabbitMQ setTimeout or other option to stop waiting for queue
- Pick a random element from an array
- Picking a random element from a set
- Picking a random element from a set
- Picking unordered combinations from pools with overlap
- Pip freeze vs. pip list
- Planar Graph Layouts

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.