I have this script that give all possible combinations from the given array.
I want to make the same thing but use each element only once.
const arr = [144, 35, 20, 10];
const sum = 65;
const findSum = (arr, sum) => {
const res = [];
const search = (index, part = []) => {
const s = part.reduce((a, b) => a + b, 0);
if (s === sum) {
res.push(part)
};
if (s >= sum || index >= arr.length) { return; };
search(index, part.concat(arr[index]));
search(index + 1, part);
};
search(0);
return res;
}
console.log(findSum(arr, sum));
the output for this example gives two combinations 0: [35, 20, 10] 1: [35, 10, 10, 10]
so i need avoid is an element multiple times like for value 10
that will give only one possible combination [35, 20, 10]
const sum = 179it callssearch()530 times... jsfiddle.net/g94e3L5d