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 JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Introduction of Firewall in Computer Network A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa 10 min read Like