How do you find all pairs of an integer array whose sum is equal to a given number in javascript or typescript

You must have seen this question in many of the interviews related to programming. This can be implemented in any language and using so many approaches. The solution I suggest is:

const findAllPairsOfAnIntegerArrayWhoseSumIsEqualToAGivenNumber = (arr, targetSum) => {
 const pairs = [];
  for(let i = 0; i< arr.length; i++) {
    for(let j = i+1;  j < arr.length; j++) {
      if(targetSum == arr[i] + arr[j]) {
          pairs.push([arr[i],arr[j]])
          console.log(`The sum of ${arr[i]} and ${arr[j]} is ${targetSum}`)
      }
    }
  }
 return pairs
}

And this is how you call it:

findAllPairsOfAnIntegerArrayWhoseSumIsEqualToAGivenNumber([0,1,2,3,4,5,6,7,8,9], 10)


Nordible is a digital solutions provider helping organizations achieve their digital goals.

We develop and measure digital solutions, implement impactful brand promotion and SEO strategies and develop software driven by rigorous testing that help your business succeed online and beyond.

You can contact us at info@nordible.com for business inquiries.

Follow us: