Check if Pair with given Sum Exists in Array using JavaScript Last Updated : 19 Feb, 2024 Comments Improve Suggest changes Like Article Like Report We will check if a pair with a given Sum exists in an Array or not using JavaScript. 2Sum ProblemThe 2sum is a famous problem asked in most interviews. We have to check whether a pair of numbers exists whose sum is equal to the target value. We are given an array of integers and the target value. We have to return true if the pair whose sum is equal to the target value exists else we have to return false. Following are the approaches Table of Content Brute Force Method Hashing MethodTwo Pointer MethodApproach 1: Brute Force Method The Two Sum issue may be solved brute-force style by using nested loops to iteratively verify every possible pair of integers in the array. However, for big datasets, this strategy is inefficient due to its O(n2) time complexity, where n is the array's size. Steps to implement above approach We use nested loop to traverse the whole array.The two loops will traverse through whole array.Find the pair if exist which will result to given sum. Example: This example implements the above-mentioned approach. JavaScript function twoSum(n, arr, target) { for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { if (arr[i] + arr[j] === target) return "True"; } } return "False"; } function main() { const n = 5; const arr = [3, 6, 4, 8, 7]; const target = 10; const ans = twoSum(n, arr, target); console.log(ans); } main(); OutputTrue Approach 2: Hashing MethodA more effective method stores the array's items and their indexes in a hash table (or dictionary). This makes constant-time lookups possible. As the method loops across the array, it determines each element's complement (goal sum minus the current element) and verifies that it is present in the hash table. If it is located, it provides the indices of the element that is now active as well as its complement; this has an O(n) time complexity. Below steps to implement above approach Create an map objects to store numbers Traverse the array using loopFor each number in the array , find the difference between target value and number. If map contains the difference between target value and number then returns true.Else returns false. Example: This example implements the above-mentioned approach. JavaScript function twoSum(n, arr, target) { const map = new Map(); for (let i = 0; i < n; i++) { const num = arr[i]; const moreNeeded = target - num; if (map.has(moreNeeded)) { return "True"; } map.set(num, i); } return "False"; } function main() { const n = 5; const arr = [3, 6, 4, 8, 7]; const target = 10; const ans = twoSum(n, arr, target); console.log(ans); } main(); OutputTrue Approach 3: Two Pointer MethodIn this approach we use two pointer to find if the target value exist in the array or not. We will use two pointer left and right initialized with the value of 0 and n-1 respectively. We will traverse until left pointer crosses the right pointer and find if the pair with sum value equal to target exist. If the pair exist then we will return true else return false. Below steps to implement above approach Sort the array.Initialize the two pointers left and right , with left = 0 and right = n-1. Traverse the loop until left crosses right.If any pair with sum equal to target value exist , return true.else return false.Example: This example implements the above-mentioned approach. JavaScript function twoSum(n, arr, target) { arr.sort((a, b) => a - b); let left = 0, right = n - 1; while (left < right) { const sum = arr[left] + arr[right]; if (sum === target) { return "True"; } else if (sum < target) { left++; } else { right--; } } return "False"; } function main() { const n = 5; const arr = [3, 6, 4, 8, 7]; const target = 10; const ans = twoSum(n, arr, target); console.log(ans); } main(); OutputTrue Comment More infoAdvertise with us Next Article Check if Pair with given Sum Exists in Array using JavaScript bug8wdqo Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Check if an Item Exists in a Multidimensional Array in JavaScript? To check if an item exists in a multidimensional array in JavaScript, you typically need to use a nested loop, or modern array methods such as Array.prototype.some(), to traverse each sub-array. Here are several ways to achieve this:1. Using Nested LoopsThe traditional way to check for an item in a 3 min read Javascript Program for Find k pairs with smallest sums in two arrays Given two integer arrays arr1[] and arr2[] sorted in ascending order and an integer k. Find k pairs with smallest sums such that one element of a pair belongs to arr1[] and other element belongs to arr2[]Examples: Input : arr1[] = {1, 7, 11} arr2[] = {2, 4, 6} k = 3Output : [1, 2], [1, 4], [1, 6]Exp 3 min read How to Check if an Element Exists in an Array in JavaScript? Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array. 2 min read Javascript Program to Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'.Note: Duplicate pairs are also allowed. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5}, 2 min read How to check a string data type is present in array using JavaScript ? In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator retu 3 min read Sum of Squares of Even Numbers in an Array using JavaScript JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array 2 min read How to find every element that exists in any of two given arrays once using JavaScript ? In this article, we will learn how to find every element that exists in any of the given two arrays. To find every element that exists in any of two given arrays, you can merge the arrays and remove any duplicate elements. Table of Content Using SetUsing loopUsing filter() and concat()Using reduce a 3 min read JavaScript - Check if JS Array Includes a Value? To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false.1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the s 4 min read Javascript Program to Find a pair with the given difference Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples: Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78Output: Pair Found: (2, 80)Input: arr[] = {90, 70, 20, 80, 50}, n = 45Output: No Such PairNative Approach:The simplest method is t 4 min read How to Check a Value Exist at Certain Array Index in JavaScript ? Determining if a value exists at a specific index in an array is a common task in JavaScript. This article explores various methods to achieve this, providing clear examples and explanations to enhance your coding efficiency.Below are the different ways to check if a value exists at a specific index 5 min read Like