Finding all possible combinations from an array in JavaScript


In the given problem statement we are asked to find all the possible combinations from an array with the help of javascript functionalities. So for this we will be working with an empty array and add all the possible combinations into this array.

Logic for The Above Problem

The easiest way to find the possible combinations from an array in javascript is by using a for loop add the combination to the created new array..

To understand the logic for this implementation we will work by iterating through the input array and for each element we will create a new combination by adding that item to each existing combination. This process will be done by iterating through the current list of combinations and creating a new combination that consists of the current combination with the new element added to the end. This newly created combination is then added to the end of the resultant array.

For example, we have the input array arr = [1, 2, 3].

We will start with an empty array as our initial list of combinations. Then we will iterate through each element in arr and for every item we will iterate the current list of combinations and add that element to create a new combinations.

  For num = 3, Possible Combination is: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

 For num = 4, Possible Combination is: [ 4 ], [ 1, 4 ], [ 2, 4 ], [ 1, 2, 4 ], [ 3, 4 ], [ 1, 3, 4 ], [ 2, 3, 4 ], [ 1, 2,     3, 4 ]

Algorithm

Step 1 − The task is to find the combinations from an array and add them in a new array of subarray. For processing this concept we will define a function and name it as possibleCombination with an argument arr.

Step 2 − In the next line declare an empty array of subarrays and name it result. This array will hold the input array's resultant potential combinations.

Step 3 − Create a for loop to cycle through the array's elements and store the length of the result in the len variable.

Step 4 − Moving on to the third step, construct another for loop that will be used to combine the input pieces.

Step 5 − Display the results as a consequence of all conceivable combinations.

Example 

// define function to return the possible combinations
function posibleCombination(arr) { 
    const result = [[]];
    for (let num of arr) { 
      const len = result.length;
      for (let i = 0; i < len; i++) {
        const temp = result[i].slice();
        temp.push(num);
        result.push(temp);
      }
    }
    return result;
  }
  // print input array and output the combinations
  const arr = [1, 2, 3];
  console.log(posibleCombination(arr));

Output

[
  [],       [ 1 ],
  [ 2 ],    [ 1, 2 ],
  [ 3 ],    [ 1, 3 ],
  [ 2, 3 ], [ 1, 2, 3 ]
]

So we can see the combinations above:

For num = 1 there are 2 possible combination [] and [1]

For num = 2 there are 4 possible combinations [], [1], [2] and [1, 2].

For num = 3 there are 8 possible combinations [], [1], [2], [1, 2], [3], [1, 3], [2, 3], and [1, 2, 3]

We can see here, for number value 1 there are 2 possible combinations. For number 2 there are 4 possible combinations and for number 3 there are 8 possible combinations.

Time Complexity

Now it's time to calculate the time complexity for the above algorithm. For our code time complexity will be O(2^n). Here n denotes the size of the array. This is because the number of alternative combinations for each item in the array is twice. The total combinations as an output with an input array of length n are 2^n.

Conclusion

In this code the possible combinations depend on the input array. If the input array is large then the time taken to make the combinations will be higher than the expected. So we can optimize it in the future as per the requirement.

Updated on: 23-Aug-2023

921 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements