Skip to content

Instantly share code, notes, and snippets.

@cassiocardoso
Created September 1, 2017 16:54
Show Gist options
  • Save cassiocardoso/d77c7917a1969a99b5766b0023d7d6f3 to your computer and use it in GitHub Desktop.
Save cassiocardoso/d77c7917a1969a99b5766b0023d7d6f3 to your computer and use it in GitHub Desktop.
Unique triples problem
const array = [-1, 0, 1, 2, -1, -4];
const length = array.length;
const sum = 0;
const solutions = [];
array.sort();
array.map((d, i) => {
let j = i + 1;
let k = length - 1;
while (j < k) {
if (array[i] + array[j] + array[k] === sum) {
solutions.push([array[i], array[j], array[k]]);
j++;
k--;
} else {
j++;
}
}
});
console.log('solution', solutions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment