I am trying to implement Given a set of distinct integers, S, return all possible subsets problem.
[A, B, C] should give [ [ ], [A], [A, B], [A, B, C], [A, C], [B], [B, C], [C] ].
Cannot figure out why the function does not backtrack.
For input [A, A, B, C], my implementation outputs [ [], ["A"], ["A", "A"], ["A", "A", "B"], ["A", "A", "B", "C"] ]
This is my code.
var subsets = function(A){
/*for input [ A, A, B, C ], appendCount returns [ [A, 2], [B, 1], [C,1] ]*/
var arr = appendCount(A);
result =[[]];
var tempArr = [];
var findSubsets = function(index, tempArr, a) {
var arrCopy = a.slice();
while(index < arrCopy.length) {
if(arrCopy[index][1] > 0) {
tempArr.push(arrCopy[index][0]);
result.push(tempArr.slice());
var newArr = arrCopy.slice();
newArr[index][1] -= 1;
findSubsets(index, tempArr, newArr);
tempArr.pop();
index++;
} else {
index++;
}
}
}
findSubsets(0, tempArr, arr.slice());
return result;
}
Thanks in advance