JavaScript arrays - Finding the number of combinations of 2 elements - Stack Overflow

I e from a Ruby background, which features anenumerable class. In Ruby, I can easily find binations o

I e from a Ruby background, which features an enumerable class. In Ruby, I can easily find binations of array elements.

arraybination(2).count

I know that JavaScript doesn't feature such built in functions, so I was wondering how I could implement this in JS. I was thinking something like

I have an array as follows

var numbers = [9,7,12]
var bos = []
for (var i = 0; i < numbers.length; i++)  {
  bos.push([numbers[i], numbers[i+1])
}

By the way, the possible bos are

[9,7], [9,12] and [7,12]

so by calling the length function on this array, 3 would be returned.

Any ideas?

I e from a Ruby background, which features an enumerable class. In Ruby, I can easily find binations of array elements.

array.bination(2).count

I know that JavaScript doesn't feature such built in functions, so I was wondering how I could implement this in JS. I was thinking something like

I have an array as follows

var numbers = [9,7,12]
var bos = []
for (var i = 0; i < numbers.length; i++)  {
  bos.push([numbers[i], numbers[i+1])
}

By the way, the possible bos are

[9,7], [9,12] and [7,12]

so by calling the length function on this array, 3 would be returned.

Any ideas?

Share Improve this question asked Mar 20, 2015 at 14:33 Josh WintersJosh Winters 511 silver badge3 bronze badges 2
  • Does order matter? ie: [9,7] and [7,9], are they considered distinct? – EyeOfTheHawks Commented Mar 20, 2015 at 14:36
  • I think that would be considered a repeated bination. Both of them consider the same two numbers, so they should be the same. I'm looking for all binations of different numbers – Josh Winters Commented Mar 20, 2015 at 14:37
Add a ment  | 

3 Answers 3

Reset to default 4

How about:

for (var i = 0; i < numbers.length; i++)
    for (var j = i + 1; j < numbers.length; j++)
        bos.push([numbers[i], numbers[j]]);

Are you strictly talking about 2-binations of the array or are you interested in a k-binations solution?

Found this in this gist

function k_binations(set, k) {
var i, j, bs, head, tailbs;

if (k > set.length || k <= 0) {
    return [];
}

if (k == set.length) {
    return [set];
}

if (k == 1) {
    bs = [];
    for (i = 0; i < set.length; i++) {
        bs.push([set[i]]);
    }
    return bs;
}

// Assert {1 < k < set.length}

bs = [];
for (i = 0; i < set.length - k + 1; i++) {
    head = set.slice(i, i+1);
    tailbs = k_binations(set.slice(i + 1), k - 1);
    for (j = 0; j < tailbs.length; j++) {
        bs.push(head.concat(tailbs[j]));
    }
}
return bs;
}

Here's a recursive function, which should work for any number:

function bination(arr, num) {
  var r= [];

  for(var i = 0 ; i < arr.length ; i++) {
    if(num===1) r.push([arr[i]]);
    else {
      bination(arr.slice(i+1), num-1).forEach(function(val) {
        r.push([].concat(arr[i], val));
      });
    }
  }
  return r;
} //bination

Working Fiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744999319a4605394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信