Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JS two numbers in array whose sum equals a given number

let twoSum = (array, sum) => {
    let hashMap = {},
      results = []

        for (let i = 0; i < array.length; i++){
            if (hashMap[array[i]]){
                results.push([hashMap[array[i]], array[i]])
            }else{
                hashMap[sum - array[i]] = array[i];
            }
          }
          return results;
    }
console.log(twoSum([10,20,10,40,50,60,70,30],50));
Comment

find if two elements in array sum to given integer js

function twoSum(arr, S) {
 const sum = [];
  for(let i = 0; i< arr.length; i++) {
    for(let j = i+1;  j < arr.length; j++) {
      if(S == arr[i] + arr[j]) sum.push([arr[i],arr[j]])
    }
  }
 return sum
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: if variable is string javascript 
Javascript :: axios check 401 run function 
Javascript :: js count char frequency in string 
Javascript :: javascript await 
Javascript :: xpath nodejs 
Javascript :: jquery: get checkbox from each row of the table and select it 
Javascript :: github pages react route 
Javascript :: convert an array to uppercase or lowercase js 
Javascript :: js day monday tuesday wednesday 
Javascript :: CocoaPods could not find compatible versions for pod "React/Core": In Podfile: react-native-fetch-blob (from `../node_modules/react-native-fetch-blob`) was resolved to 0.10.6, which depends on React/Cor 
Javascript :: min and max javascript 
Javascript :: js join array 
Javascript :: atob javascript 
Javascript :: find common characters in two strings javascript 
Javascript :: reverse a string in javascript 
Javascript :: how to use jszip in node.js 
Javascript :: react alert popup 
Javascript :: javascript rock paper scissors 
Javascript :: momentjs 
Javascript :: json to formdata 
Javascript :: ReactJS Axios Delete Request Code Example 
Javascript :: react using proptypes 
Javascript :: img tag in react 
Javascript :: loop object array 
Javascript :: what is last index of array 
Javascript :: jquery each hover 
Javascript :: aggregate mongodb 
Javascript :: usecallback react 
Javascript :: document.addEventListener("backbutton 
Javascript :: javascript object loop 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =