JavaScript数组 - 查找2个元素的组合数

时间:2022-08-22 14:12:35

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

我来自Ruby背景,它具有可枚举的类。在Ruby中,我可以很容易地找到数组元素的组合。

array.combination(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

我知道JavaScript没有这样的内置函数,所以我想知道如何在JS中实现它。我在想类似的东西

I have an array as follows

我有一个数组如下

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

By the way, the possible combos are

顺便说一下,可能的组合是

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

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

所以通过在这个数组上调用length函数,将返回3。

Any ideas?

3 个解决方案

#1


3  

How about:

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

#2


1  

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

您是否严格谈论阵列的2种组合,或者您对k组合解决方案感兴趣?

Found this in this gist

在这个要点中找到了这个

function k_combinations(set, k) {
var i, j, combs, head, tailcombs;

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

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

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

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

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

#3


0  

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

这是一个递归函数,它适用于任何数字:

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

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

Working Fiddle

#1


3  

How about:

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

#2


1  

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

您是否严格谈论阵列的2种组合,或者您对k组合解决方案感兴趣?

Found this in this gist

在这个要点中找到了这个

function k_combinations(set, k) {
var i, j, combs, head, tailcombs;

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

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

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

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

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

#3


0  

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

这是一个递归函数,它适用于任何数字:

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

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

Working Fiddle