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 :: javascript kill all childs 
Javascript :: next js environment variables 
Javascript :: cheat sheet javascript 
Javascript :: arrays inside array of objects 
Javascript :: upgrade or update nodejs 
Javascript :: javascript slice string from character 
Javascript :: convert int to string in angular 
Javascript :: js loop through function arguments 
Javascript :: moment.set 
Javascript :: javascript input file callback 
Javascript :: ng-class equivalent in react 
Javascript :: get bytes from string javascript 
Javascript :: null data type in javascript 
Javascript :: react concatenate string and component 
Javascript :: shorthand if statement js 
Javascript :: how get count of letters in javascript 
Javascript :: try catch with for loop in javascript 
Javascript :: Solution for Error [ERR_REQUIRE_ESM]: require() of ES Module 
Javascript :: detect keyboard open or close in react js 
Javascript :: cra proxy 
Javascript :: md 5 npm 
Javascript :: tailwind container class size 
Javascript :: how to get the text of a clicked elemet by javascript 
Javascript :: pagination.js example codepen 
Javascript :: string contains at least one number 
Javascript :: cors axios 
Javascript :: javascript formdata 
Javascript :: Node.JS mongodb create database 
Javascript :: button disappears after click javascript 
Javascript :: react functional components shortcut in webstorm 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =