programming-examples/js/Functions/Write a JavaScript function to get all possible subset with a fixed length (for example 2) combinations in an array..js
2019-11-15 12:59:38 +01:00

28 lines
557 B
JavaScript

function subset(arra, arra_size)
{
var result_set = [],
result;
for(var x = 0; x < Math.pow(2, arra.length); x++)
{
result = [];
i = arra.length - 1;
do
{
if( (x & (1 << i)) !== 0)
{
result.push(arra[i]);
}
} while(i--);
if( result.length >= arra_size)
{
result_set.push(result);
}
}
return result_set;
}
console.log(subset([1, 2, 3], 2));