Skip to content

Instantly share code, notes, and snippets.

@dmi3y
Created February 23, 2015 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3y/b6bbc01c58d85375cdff to your computer and use it in GitHub Desktop.
Save dmi3y/b6bbc01c58d85375cdff to your computer and use it in GitHub Desktop.
function findSumPairs(arr, sum) {
'use strict';
var
i = 0,
j = 0,
isum,
imatch,
out = [],
larr;
larr = arr.slice(0); // shallow copy (just in case)
for ( i = 0; i < larr.length; i++ ) {
for ( j = 0; j < larr.length; j++ ) {
if ( i !== j ) {
isum = larr[i] + larr[j];
if ( isum === sum ) {
imatch = [larr[i], larr[j]];
out.push(imatch);
larr.splice(i, 1);
larr.splice(j, 1);
break;
}
}
}
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment