42
rated 0 times
[
46]
[
4]
/ answers: 1 / hits: 121040
/ 7 Years ago, wed, april 5, 2017, 12:00:00
I've seen several similar questions about how to generate all possible combinations of elements in an array. But I'm having a very hard time figuring out how to write an algorithm that will only output combination pairs. Any suggestions would be super appreciated!
Starting with the following array (with N elements):
var array = [apple, banana, lemon, mango];
And getting the following result:
var result = [
apple banana
apple lemon
apple mango
banana lemon
banana mango
lemon mango
];
I was trying out the following approach but this results in all possible combinations, instead only combination pairs.
var letters = splSentences;
var combi = [];
var temp= ;
var letLen = Math.pow(2, letters.length);
for (var i = 0; i < letLen ; i++){
temp= ;
for (var j=0;j<letters.length;j++) {
if ((i & Math.pow(2,j))){
temp += letters[j]+
}
}
if (temp !== ) {
combi.push(temp);
}
}
More From » arrays