Question d’entretien chez Ness Digital Engineering

Coding challenge: “Find all pairs of elements in an array whose sum equals X.”

Réponse à la question d'entretien

Utilisateur anonyme

21 sept. 2025

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; }