
- 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
Is there any more efficient way to code this “2 Sum” Questions JavaScript
Our job is to write a function that solves the two-sum problem in at most linear time.
Two Sum Problem
Given an array of integers, we have to find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array.
Solving the problem in O(n) time
We will use a hashmap to keep a record of the items already appeared, on each pass we will check whether there exists any element in the map which when added to the current element add up to the target, if there are any we will return an array containing their indices and if we go through the whole loop without satisfying this condition, we will return an empty array.
Example
const arr = [2, 5, 7, 8, 1, 3, 6, 9, 4]; const sum = 10; const twoSum = (arr, sum) => { const map = {}; for(let i = 0; i < arr.length; i++){ const el = sum - arr[i]; if(map[el]){ return [map[el], i]; }; map[arr[i]] = i; }; return []; }; console.log(twoSum(arr, sum)); console.log(twoSum(arr, 12)); console.log(twoSum(arr, 13)); console.log(twoSum(arr, 14)); console.log(twoSum(arr, 24));
Output
The output in the console will be −
[ 2, 5 ] [ 1, 2 ] [ 1, 3 ] [ 3, 6 ] []
- Related Articles
- Is there any way to check if there is a null value in an object or array in JavaScript?
- Is there any way to skip some documents in MongoDB?
- What is the most efficient way to deep clone an object in JavaScript?
- Is there any way I can call the validate() function outside the initValidation() function in JavaScript?
- Writing Efficient Python Code
- Writing Efficient R Code
- Is there any way to embed a PDF file into an HTML5 page?
- Is there any way to see the MongoDB results in a better format?
- Is there any easy way to add multiple records in a single MySQL query?
- Is there any way in MongoDB to get the inner value of json data?
- Is there any way to load an extension in chrome browser using Selenium Webdriver?
- Efficient way to remove all entries from MongoDB?
- Is there any way of moving to HTML 5 and still promise multi browser compatibility?
- What is the most efficient way to concatenate many Python strings together?
- Is there a way to print all methods of an object in JavaScript?
