Last active
January 14, 2026 21:59
-
-
Save ngimdock/240e9c824bea22fe53a09e50511cdeb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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