JavaScript Program to Find Pairs with Given Sum
function findPairs(arr, sum) { const seen = new Set(); const pairs = []; for (const num of arr) { const complement = sum - num; if (seen.has(complement)) { pairs.push([complement, num]); } seen.add(num); } return pairs; }Examples
How to Think About It
Algorithm
Code
function findPairs(arr, sum) { const seen = new Set(); const pairs = []; for (const num of arr) { const complement = sum - num; if (seen.has(complement)) { pairs.push([complement, num]); } seen.add(num); } return pairs; } const array = [1, 2, 3, 4, 5]; const targetSum = 5; console.log(findPairs(array, targetSum));
Dry Run
Let's trace the example array [1, 2, 3, 4, 5] with sum 5 through the code.
Start with empty set and pairs
seen = {}, pairs = []
Process number 1
complement = 5 - 1 = 4; 4 not in seen; add 1 to seen; seen = {1}
Process number 2
complement = 5 - 2 = 3; 3 not in seen; add 2 to seen; seen = {1, 2}
Process number 3
complement = 5 - 3 = 2; 2 in seen; add pair [2, 3]; pairs = [[2, 3]]; add 3 to seen; seen = {1, 2, 3}
Process number 4
complement = 5 - 4 = 1; 1 in seen; add pair [1, 4]; pairs = [[2, 3], [1, 4]]; add 4 to seen; seen = {1, 2, 3, 4}
Process number 5
complement = 5 - 5 = 0; 0 not in seen; add 5 to seen; seen = {1, 2, 3, 4, 5}
Return pairs
pairs = [[2, 3], [1, 4]]
| Number | Complement | Complement in seen? | Pairs | Seen Set |
|---|---|---|---|---|
| 1 | 4 | No | [] | {1} |
| 2 | 3 | No | [] | {1, 2} |
| 3 | 2 | Yes | [[2, 3]] | {1, 2, 3} |
| 4 | 1 | Yes | [[2, 3], [1, 4]] | {1, 2, 3, 4} |
| 5 | 0 | No | [[2, 3], [1, 4]] | {1, 2, 3, 4, 5} |
Why This Works
Step 1: Track seen numbers
We use a Set to remember numbers we have already checked, so we can quickly find if the needed partner exists.
Step 2: Find complement
For each number, we calculate the complement by subtracting it from the target sum to know what number pairs with it.
Step 3: Check and store pairs
If the complement is already in the set, we add the pair to the result because together they make the target sum.
Alternative Approaches
function findPairsBruteForce(arr, sum) { const pairs = []; for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] + arr[j] === sum) { pairs.push([arr[i], arr[j]]); } } } return pairs; } console.log(findPairsBruteForce([1,2,3,4,5], 5));
function findPairsTwoPointers(arr, sum) { arr.sort((a, b) => a - b); const pairs = []; let left = 0, right = arr.length - 1; while (left < right) { const currentSum = arr[left] + arr[right]; if (currentSum === sum) { pairs.push([arr[left], arr[right]]); left++; right--; } else if (currentSum < sum) { left++; } else { right--; } } return pairs; } console.log(findPairsTwoPointers([1,2,3,4,5], 5));
Complexity: O(n) time, O(n) space
Time Complexity
The main approach loops through the array once, checking and adding to a set in constant time, so it runs in O(n) time.
Space Complexity
It uses extra space for the set and pairs list, which can grow up to O(n) in the worst case.
Which Approach is Fastest?
The set-based approach is faster than brute force (O(n^2)) and avoids sorting overhead, making it best for unsorted arrays.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Set Lookup | O(n) | O(n) | Unsorted arrays, fast lookup |
| Brute Force | O(n^2) | O(1) | Very small arrays or simple code |
| Two Pointers | O(n log n) | O(1) | Sorted arrays or when sorting is cheap |