Ads Top

Interview Practice : Javascript Two Sum Solution

Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.

TLDR; - Solution:
Use a hashtable to store the values and use it as a lookup to see if the "target value" exits Key Terms
Hashmap: A way to store and lookup data quickly using keys ( and values ).
The key:value storage format allows to have direct access to its items in constant time.

Constant Time: A calculation that is not dependant on input size. Meaning the data set can be as large as you want and the time complexity will be O(1). This is the most optimal BIG O notation scenario. It will use the shortest amount of time or least space to return a result.

Implement a hashmap in Javascript
Technically Javascript does not have a native hashmap function but it does have native Map() function you can use. You can also use a standard object {}

Built in Map() function JS Object Hash Example

No comments:

Powered by Blogger.