Skip to content

Instantly share code, notes, and snippets.

@hddananjaya
Created December 18, 2019 05:24
Show Gist options
  • Save hddananjaya/c16f1e25f1a903ffc50c5365d29e9ab3 to your computer and use it in GitHub Desktop.
Save hddananjaya/c16f1e25f1a903ffc50c5365d29e9ab3 to your computer and use it in GitHub Desktop.
Return all combination pairs.
function getPairs(arr){
var pairsList = [];
for (var i=0; i < arr.length - 1; i++){
for (var j=i+1; j < arr.length; j++){
pairsList.push([arr[i], arr[j]]);
}
}
return (pairsList);
}
var S = [1, 7, 2, 4];
console.log(getPairs(S));
// [[1, 7], [1, 2], [1, 4], [7, 2], [7, 4], [2, 4]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment