-1

I am trying to write a function that will take an array and n as parameters,

it will return all subsets of that array with n elements, have tried a couple things, couldn't yet succeed.

thanks to whoever put it here, this functions is way too complicated and doesn't do the job, basically what I tried to do here is to pick out one element from a 4 element array to create its 3 element subsets. It doesn't even take N as parameter. it returns all 3 element subsets but also identical ones, so I have to filter them out as well, in any case I will keep trying.

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]);
          } 
       }
    } 
}
Censorry
  • 21
  • 3
  • 1
    Show us what you tried and we'll help you to fix it and understand why it doesn't work. – Sebastien C. Jul 03 '14 at 14:50
  • This doesn't even take N as parameter it was just experimentation: I am still trying 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]); } } } } – Censorry Jul 03 '14 at 14:59
  • Edit your post and put what you have tried there. It is not formatted in the comments and difficult to read. – jluckin Jul 03 '14 at 15:01
  • 1
    See this question: http://stackoverflow.com/questions/5752002/find-all-possible-subset-combos-in-an-array – River Tam Jul 03 '14 at 15:15
  • @RiverTam, that link is very helpful and relevant to this question, thanks for finding it. Use the results from that function, and then filter it so only subsets of length N remain. – jluckin Jul 03 '14 at 15:21

2 Answers2

2

That not as complicated as it seems. This one is optimized because it doesn't creates useless temporary arrays during the process.

function findSubsets(array, n) { 
    var answers = []; 
    for(var i = 0 ; i < array.length ; i += n) {
        answers.push(array.slice(i, i + n));
    }
    return answers;
}

findSubsets([1, 2, 3, 4, 5, 6, 7, 8, 9], 2) // --> [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
findSubsets([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) // --> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Sebastien C.
  • 4,649
  • 1
  • 21
  • 32
  • 1
    thanks but this one doesn't return all subsets, so no [1,3], [1,5], I saw a solution for this with bit operators, – Censorry Jul 04 '14 at 19:46
1

You can try this solution

var subsetArray = (function() {
  return {
    getResult: getResult
  }

  function getResult(array, n) {

    function isBigEnough(value) {
      return value.length === n;
    }

    var ps = [
      []
    ];
    for (var i = 0; i < array.length; i++) {
      for (var j = 0, len = ps.length; j < len; j++) {
        ps.push(ps[j].concat(array[i]));
      }
    }
    return ps.filter(isBigEnough);
  }
})();

var arr = [1, 2, 3, 4,5,6,7,8,9]; console.log(subsetArray.getResult(arr,2));

Thillai Narayanan
  • 4,766
  • 5
  • 32
  • 39