

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pair whose sum exists in the array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The function should pick a pair of two numbers which are at different indices (consecutive or nonconsecutive) whose sum also exists in the array.
Example
Following is the code −
const arr = [1, 3, 5, 6, 8, 9]; const findPair = (arr = []) => { let count = 0; for(let i = 0; i < arr.length; i++){ for(let j = 0; j < arr.length; j++){ if(i === j){ break; }; let sum = arr[i] + arr[j]; if(arr.includes(sum)){ return [arr[i], arr[j]]; }; }; }; return []; }; console.log(findPair(arr));
Output
Following is the output on console −
[5, 1]
- Related Questions & Answers
- Pair of (adjacent) elements of an array whose sum is lowest JavaScript
- Count number of distinct pairs whose sum exists in the given array in C++
- Achieving maximum possible pair sum in JavaScript
- Find required sum pair with JavaScript
- Find the largest pair sum in an unsorted array in C++
- Adjacent elements of array whose sum is closest to 0 - JavaScript
- Finding matching pair from an array in JavaScript
- How to find a group of three elements in an array whose sum equals some target sum JavaScript
- Count pairs in array whose sum is divisible by 4 in C++
- Count pairs in array whose sum is divisible by K in C++
- XOR of Sum of every possible pair of an array in C++
- Reverse sum array JavaScript
- JavaScript - Convert an array to key value pair
- Best way to find two numbers in an array whose sum is a specific number with JavaScript?
- Recursion - Sum Nested Array in JavaScript
Advertisements