Skip to content

Instantly share code, notes, and snippets.

@sarbull
Last active September 24, 2017 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarbull/cde3eac3567560df151420983aaac58a to your computer and use it in GitHub Desktop.
Save sarbull/cde3eac3567560df151420983aaac58a to your computer and use it in GitHub Desktop.
function pairs(array, k) {
var hash = {};
var length = array.length;
var result = [];
for(var i = 0; i < length; i++) {
if(hash[array[i]]) {
hash[array[i]]++;
} else {
hash[array[i]] = 1;
};
}
console.log(hash);
for(i = 0; i < length; i++){
if(hash[k - array[i]] > 1) {
hash[k - array[i]]--;
console.log(array[i], k - array[i]);
}
}
}
pairs([1, 2, 2, 3, 4, 5], 3);
pairs([1, 10, 12], 2);
/* Results
> node test.js
{ '1': 1, '2': 2, '3': 1, '4': 1, '5': 1 }
1 2
{ '1': 1, '10': 1, '12': 1 }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment