Coding challenge: “Find all pairs of elements in an array whose sum equals X.”
Sigiloso
function pairsWithSum(arr, target) { const seen = new Map(); const res = []; for (let num of arr) { const comp = target - num; if (seen.get(comp) > 0) { res.push([comp, num]); seen.set(comp, seen.get(comp) - 1); } else { seen.set(num, (seen.get(num) || 0) + 1); } } return res; }