- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# 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. 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.

- Related Articles
- How to get all combinations of some arrays in JavaScript?
- All combinations of sums for array in JavaScript
- Finding all possible combinations from an array in JavaScript
- Possible combinations and convert into alphabet algorithm in JavaScript
- Generate all combinations of supplied words in JavaScript
- Find all substrings combinations within arrays in JavaScript
- JavaScript function that should count all unique items in an array
- Python program to get all pairwise combinations from a list
- How to get all unique values in a JavaScript array?
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- How to get all the combinations of the keypad value in a mobile by backtracking using C#?
- Add property to common items in array and array of objects - JavaScript?
- JavaScript function that generates all possible combinations of a string
- Sort an array to have specific items first in the array - JavaScript
- What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?