
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Best way to find two numbers in an array whose sum is a specific number with JavaScript?
Let’s say the following is our array −
var numbers = [10,3,40,50,20,30,100]
We need to search two numbers from the above array elements, whose sum is 80.
For this, use simple for loop with if condition.
Example
function specificPairsOfSumOfTwoNumbers(numbers, totalValue) { var storeTwoNumbersObject = {} for(var currentNumber of numbers) { if(storeTwoNumbersObject[currentNumber]) { return { firstNumber: totalValue-currentNumber, secondNumber:currentNumber} } storeTwoNumbersObject[totalValue-currentNumber] = true; } return false; } var numbers = [10,3,40,50,20,30,100] console.log("The Two numbers which has the sum 80="); console.log(specificPairsOfSumOfTwoNumbers(numbers, 80) )
To run the above program, you need to use the following command −
node fileName.js.
Here my file name is demo207.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo207.js The Two numbers which has the sum 80= { firstNumber: 50, secondNumber: 30 }
- Related Articles
- Get the Triplets in an Array Whose Sum is Equal to a Specific Number in Java
- Best way to flatten an object with array properties into one array JavaScript
- Finding two prime numbers with a specific number gap in JavaScript
- Finding two closest elements to a specific number in an array using JavaScript
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- What is the best way to initialize a JavaScript number?
- Give an example of each of two irrational numbers whose sum is a rational number.
- Best way to sum array size fields in MongoDB?
- Find two consecutive whole numbers whose sum is 19.
- Give an example of each of two irrational numbers whose sum is an irrational number.
- How to find specific words in an array with JavaScript?
- What is the best way to compare two strings in JavaScript?
- Find two numbers whose product is $-24$ and sum is 2.
- Find two numbers whose sum is 27 and product is 182.
- What is the best way to convert a number to a string in JavaScript?

Advertisements