
- 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
Finding all possible combinations from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a target sum as the second argument. The function should construct an array of array of all such elements from the array (repeating or non-repeating) that adds up to the target sum.
For example − If the input array is −
const arr = [2, 3, 6, 7], sum = 7;
Therefore, the output for the above input should look like this −
const output = [ [2, 2, 3], [7] ];
Example
The code for this will be −
const arr = [2, 3, 6, 7], sum = 7; const combineElements = (arr, sum) => { const output = []; const findCombination = (remain, path, start) => { if (remain < 0) { return; } if (remain === 0) { output.push([...path]); return; } for (let i = start; i < arr.length; i++) { findCombination(remain − arr[i], [...path, arr[i]], i); } } findCombination(sum, [], 0); return output; }; console.log(combineElements(arr, sum));
Output
And the output in the console will be −
[ [ 2, 2, 3 ], [ 7 ] ]
- Related Articles
- Finding all possible subsets of an array in JavaScript
- Finding all the longest strings from an array in JavaScript
- All combinations of sums for array in JavaScript
- JavaScript function that generates all possible combinations of a string
- Finding matching pair from an array in JavaScript
- Finding all possible ways of integer partitioning in JavaScript
- Finding even length numbers from an array in JavaScript
- Finding all peaks and their positions in an array in JavaScript
- Finding all duplicate numbers in an array with multiple duplicates in JavaScript
- Finding the nth missing number from an array JavaScript
- Algorithm to get the combinations of all items in array JavaScript
- Take an array of integers and create an array of all the possible permutations in JavaScript
- Generating all possible permutations of array in JavaScript
- Print all possible combinations of r elements in a given array of size n in C++
- Finding unlike number in an array - JavaScript

Advertisements