DEV Community

Discussion on: How do you find all pairs of an integer array whose sum is equal to a given number?

Collapse
 
anduser96 profile image
Andrei Gatej

Another solution would be to use a map(or a dictionary in python, AFAIK) where each key would follow this rule: sum - list[i];

for (let i = 0; i < len; i++) {
   const k = sum-list[i];
  if (map.has(k))
     console.log(k, map.get(k))
  else map.set(k, list[i]);
}