Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Algorithm to get the combinations of all items in array JavaScript
In this problem statement, our task is to get the combinations of all items in an array with the help of Javascript functionalities. We can use a recursive approach that iteratively adds items to a running list of combinations.
Understanding the Problem
The goal is to write a function in Javascript that finds all possible combinations of elements in an array. For example, if we have an array [1, 2, 3], the combinations would be:
[ [], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3] ]
Note that combinations include the empty set and all possible subsets of the original array.
Algorithm Steps
Step 1 ? Declare a function called getCombinations which takes an array parameter.
Step 2 ? Declare a result array inside the function to hold the final list of combinations.
Step 3 ? Define a recursive function called recurse with two parameters: cur (current combination) and rem (remaining items).
Step 4 ? If no items are left in rem, push the current combination to the result array.
Step 5 ? Otherwise, iterate through each item in rem and recursively call the function with updated parameters.
Step 6 ? Initialize the recursion with an empty array and the full input array.
Step 7 ? Return the result array containing all combinations.
Implementation
// Function to get all combinations of input array
function getCombinations(array) {
const result = [];
function recurse(cur, rem) {
if (rem.length === 0) {
result.push(cur);
} else {
for (let i = 0; i < rem.length; i++) {
recurse([...cur, rem[i]], rem.slice(i + 1));
}
}
}
recurse([], array);
return result;
}
// Example usage
const array = [10, 20, 30];
const combinations = getCombinations(array);
console.log("All combinations:");
combinations.forEach((combo, index) => {
console.log(`${index + 1}: [${combo.join(', ')}]`);
});
console.log(`Total combinations: ${combinations.length}`);
All combinations: 1: [] 2: [10] 3: [20] 4: [30] 5: [10, 20] 6: [10, 30] 7: [20, 30] 8: [10, 20, 30] Total combinations: 8
Alternative Implementation Using Bit Manipulation
For better understanding, here's an alternative approach using bit manipulation:
function getCombinationsBitwise(array) {
const result = [];
const n = array.length;
// Generate all combinations using bit manipulation
for (let i = 0; i < Math.pow(2, n); i++) {
const combination = [];
for (let j = 0; j < n; j++) {
if (i & (1 << j)) {
combination.push(array[j]);
}
}
result.push(combination);
}
return result;
}
// Example usage
const testArray = ['A', 'B', 'C'];
const bitwiseCombinations = getCombinationsBitwise(testArray);
console.log("Bitwise approach:");
bitwiseCombinations.forEach((combo, index) => {
console.log(`${index + 1}: [${combo.join(', ')}]`);
});
Bitwise approach: 1: [] 2: [A] 3: [B] 4: [A, B] 5: [C] 6: [A, C] 7: [B, C] 8: [A, B, C]
Complexity Analysis
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Recursive | O(2^n) | O(2^n) | More intuitive, easier to understand |
| Bit Manipulation | O(n × 2^n) | O(2^n) | More efficient for small arrays |
Both approaches have exponential complexity because there are 2^n possible combinations for an array of n elements.
Conclusion
The recursive approach provides an elegant solution for generating all combinations of array elements. While the O(2^n) complexity makes it less efficient for large arrays, it works well for small to medium-sized datasets. Consider the array size before using this algorithm in production applications.
