JavaScript - Find All The Combinations of Array Values Last Updated : 15 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Combinations refer to subsets of a given array, where the order of elements does not matter. For example, given an array [1, 2, 3], the possible combinations include:Single-element combinations: [1], [2], [3]Two-element combinations: [1, 2], [1, 3], [2, 3]All three elements: [1, 2, 3] Here are the different methods to find all the combinations of array values in JavaScript1. Using Recursive ApproachRecursion is used to solve the problem. The base condition is When the length of the array reduces to one then return that element of the array. JavaScript function get(arr) { const res = []; function help(curr, remain) { if (remain.length === 0) { if (curr.length > 0) { res.push(curr); } return; } help([...curr, remain[0]], remain.slice(1)); help(curr, remain.slice(1)); } help([], arr); return res; } //Driver Code Starts const inp = [1, 2, 3]; console.log(get(inp)); //Driver Code Ends Output[ [ 1, 2, 3 ], [ 1, 2 ], [ 1, 3 ], [ 1 ], [ 2, 3 ], [ 2 ], [ 3 ] ] In this exampleThe help function recursively generates all subsets by either including or excluding each element from the array.It makes two recursive calls: one that includes the current element and one that excludes it, processing until all elements are considered.When the recursion finishes, all non-empty subsets are collected in the res array and returned.2. Using Bitmasking/Iterative method for CombinationsBitmasking/ Iterative method provides an alternative approach to generating combinations. JavaScript function get(arr) { const res = []; const total = Math.pow(2, arr.length); for (let i = 1; i < total; i++) { const comb = []; for (let j = 0; j < arr.length; j++) { if (i & (1 << j)) { comb.push(arr[j]); } } res.push(comb); } return res } const inp = [1, 2, 3]; //Driver Code Starts console.log(get(inp)); //Driver Code Ends Output[ [ 1 ], [ 2 ], [ 1, 2 ], [ 3 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ] In this exampleThe get function generates all non-empty subsets by using bit manipulation, where each bit in the binary representation of numbers determines whether to include an element from the array.The outer loop iterates through all possible combinations (from 1 to 2^n - 1), and the inner loop checks each bit of the number to decide which elements to include.Each subset is added to the res array, which is returned after processing all combinations. Comment More infoAdvertise with us Next Article JavaScript - Find All The Combinations of Array Values P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads Javascript Program for Pairs such that one is a power multiple of other You are given an array A[] of n-elements and a positive integer k (k > 1). Now you have find the number of pairs Ai, Aj such that Ai = Aj*(kx) where x is an integer. Note: (Ai, Aj) and (Aj, Ai) must be count once.Examples : Input : A[] = {3, 6, 4, 2}, k = 2Output : 2Explanation : We have only two 3 min read Javascript Program for Number of unique triplets whose XOR is zero Given N numbers with no duplicates, count the number of unique triplets (ai, aj, ak) such that their XOR is 0. A triplet is said to be unique if all of the three numbers in the triplet are unique. Examples: Input : a[] = {1, 3, 5, 10, 14, 15};Output : 2 Explanation : {1, 14, 15} and {5, 10, 15} are 3 min read JavaScript Program to Construct an Array from its pair-sum Array The pair-sum array is a unique construction that holds the sum of all potential pairs of elements from the original array. At first glance, it might appear to be challenging, but in this article, we'll Construct an array from its pair-sum array and discover some of its most intriguing uses. What is 4 min read JavaScript Program to Count Reverse Pairs Given an array, array [] of N integers, find the count of reverse pairs. A pair of indices (i, j) is said to be a reverse pair if both the following conditions are met: 0 <= i < j < N arr[i] > 2 * arr[j]Examples: Input: N = 6, arr = [3, 2, 4, 5, 1, 20]Output: 3Explanation: The reverse pa 3 min read Combinations from n arrays picking one element from each array Given a list of arrays, find all combinations where each combination contains one element from each given array. Examples: Input : [ [1, 2], [3, 4] ] Output : 1 3 1 4 2 3 2 4 Input : [ [1], [2, 3, 4], [5] ] Output : 1 2 5 1 3 5 1 4 5 We keep an array of size equal to the total no of arrays. This arr 8 min read Like