Generating all combinations of elements in a single array in pairs
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 of elements from a single array is a common task in programming, often needed in mathematical computations, data analysis, and algorithm development. This article delves into generating all unique combinations of array elements in pairs, relevant use-cases, and technical explanations.
What are Combinations?
Combinations are selections made by choosing elements from a set without regard to the order in which they are selected. When discussing combinations in pairs, we are specifically referring to the task of choosing two elements from an array and forming unique pairs.
Technical Explanation
Given an array $\text\{arr\}$ of $n$ elements, a combination in pairs involves selecting all possible unique pairs of elements. If order matters, combinations are considered permutations; however, when the order does not matter (e.g., is identical to ), they are true combinations.
For instance, consider an array with 4 elements: . The possible combinations in pairs are:
• • • • • •
Mathematical Background
The number of unique pairs (combinations) can be calculated using the binomial coefficient, often denoted as , where is the total number of elements, and is the size of each combination (in this case, ). The formula is:
For combinations of pairs:
This formula provides the count of unique pairs. For the array , this results in pairs.
Algorithm to Generate Combinations
The algorithm to generate combinations involves nested iterations through the array, avoiding duplication:
- Initialization: Start with an empty result list.
- Iteration: Loop through each element of the array with an index .
- Nested Loop: For each , start a second loop from index to the end of the array.
- Collect Pairs: For each pair of indices and , add the combination to the list of combinations.
Example Code (Python)
• Time Complexity: The algorithm involves two nested loops over the array, leading to time complexity. • Space Complexity: The space complexity depends on the number of generated pairs, approximately , as each pair needs to be stored.
Related reading
- Generating All Combinations of List n Levels Deep in Java
- Generating all factors of a number given its prime factorization
- Generating all permutations excluding cyclic rotations
- Generating all permutations of a given string
- Generating ids for a set of integers
- Generating strongly-connected, uniformly-distributed, random di-graphs
- Generating all permutations of a given string
- Generating circular shifts / reduced Latin Squares in Python

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.