I've been working on creating a function that can generate subsets from an array based on a specified number of elements, but I haven't quite cracked it yet.
I came across a function that was supposed to do the job, but it turned out to be too complex and didn't quite meet my requirements. It was attempting to create subsets with three elements from a four-element array, but it wasn't even taking the number of elements (n) as a parameter. Additionally, it was generating duplicate subsets, so I needed to figure out how to filter those out.
function findSubsets(array) {
var answers = [];
var firstArray = array;
for (i = 0; i < array.length; i++) {
array = firstArray;
for (var k = 0; k < array.length; k++) {
if (k != i) {
var subset = array.splice(k, 1);
answers.push(array); array.splice(k, 0, subset[0]);
}
}
}
}