Skip to content

Instantly share code, notes, and snippets.

@ngimdock
Last active January 14, 2026 21:59
Show Gist options
  • Select an option

  • Save ngimdock/240e9c824bea22fe53a09e50511cdeb7 to your computer and use it in GitHub Desktop.

Select an option

Save ngimdock/240e9c824bea22fe53a09e50511cdeb7 to your computer and use it in GitHub Desktop.
function findUniquePairs(arr: number[], target: number): number[][] {
if (arr.length < 2) return [];
const result: number[][] = [];
const nums = [...arr].sort((a, b) => a - b);
let left = 0;
let right = nums.length - 1;
while (left < right) {
const sum = nums[left] + nums[right];
if (sum === target) {
result.push([nums[left], nums[right]]);
// Skip duplicates
const leftValue = nums[left];
const rightValue = nums[right];
while (left < right && nums[left] === leftValue) left++;
while (left < right && nums[right] === rightValue) right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment