# Simple Combine //JavaScript Repository

## Description

Mathematical combination on array elements.
Created: 2005.10.02

## Code (Download)

``````//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/array/combine [rev. #1]

function combine(a, q){
var n = a.length - 1, l = n - q + 1, x = 0, c = [], z = -1, p, j, d, i;
if(q > n || q < 2) return c;
for(p = [], i = q; p[--i] = i;);
while(x <= l){
for(c[++z] = [], j = -1; ++j < q; c[z][j] = a[p[j]]);
if(++p[j - 1] > n)
while(j--)
if(!j && x++, (d = p[j]) < l + j){
while(j < q) p[j++] = ++d;
break;
}
}
return c;
};``````

## Example (Example)

``````<script type="text/javascript">
//<![CDATA[

var a = ["A", "B", "C", "D", "E", "F"], q = 4, j = combine(a, q);
document.write(
"<h2>", a.join(" - "), " : ", q, " = ", j.length, "</h2>",
j.join("<br />")
);

//]]>
</script>``````

## Help

combine(vector: Array, n: Integer): Array
Do a mathematical combination, all the groups with a specified amount of elements possible to be grouped without repeated elements. The function returns an bidimensional array, where line = combination and column = element.
vector
array which the elements will be combined
n
amount of elements in each group

3.69