
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Combinations of All Items in Array using JavaScript
In this problem statement, our task is to get the combinations of all items in an array with the help of Javascript functionalities. So for doing this task we can use a recursive approach that iteratively adds items to a running list of combinations.
Understanding the problem statement
The problem statement is to write a function in Javascript that will help to find out the combinations of all the elements in an array and create a separate array to show these combinations. For example, if we have an array [ 1, 2 ] so the combinations of this array will be [ [ 1, 2 ], [ 2 ] ].
Logic for the given problem
To create a function to get all the possible combinations of items in an array in Javascript can be done using a recursive algorithm which will iteratively add items to a list of combinations. So first we will define an array and initialize it as empty. Inside the created function we will define another function to recurse the running list of combinations.
Algorithm
Step 1 ? Declare a function called getCombinations which is using a parameter of array.
Step 2 ? Declare a result array inside the function which will hold our final list of combinations.
Step 3 ? Define another function called recurse which will take two arguments cur and rem, here cur is the running list of items and rem is the array of items we have left to add in the combinations.
Step 4 ? And after this if there are no items left in the rem array we will push current into the result array because we have reached the required result.
Step 5 ? Otherwise we will iterate every item in the rem array and recursively call a recursive function with a new cur array that will include the current item.
Step 6 ? Call recurse initially with an empty cur array and the full array we want to generate combinations for.
Step 7 ? Return the final result array which contains all possible combinations of the input array.
Code for the algorithm
//function to get the 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, 40]; const combinations = getCombinations(array); console.log(combinations);
Complexity
The time complexity for the above created function is O(2^n) because the algorithm generates all possible combinations of items in the array and there are 2^n possible combinations. And the space complexity for the code is also O(2^n) because the algorithm generates a list of 2^n combinations.
Conclusion
The above code provides a simple solution to generate all possible combinations of items in an array in Javascript. So it has an O(2^n) time and space complexity which can make it less efficient for large arrays. So it is important to note the size of the array when deciding whether to use this algorithm.