Two Sum in JavaScript

Two Sum in JavaScript

Introduction

I am still on the journey of solving at least one Data Structures and Algorithm question daily on Leetcode in order to understand the concepts I am learning well and it has been very challenging and interesting since I started solving these questions.

This article will explain my solution to Two Sum Question on Leetcode and the logic behind my solution. Try to follow it step by step and I am sure you will understand every bit of it.

Let's dive into it ๐ŸŽ‰

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.

Example 1:

  • Input: nums = [2,7,11,15], target = 9
  • Output: [0,1]
  • Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

  • Input: nums = [3,2,4], target = 6
  • Output: [1,2]

Example 3:

  • Input: nums = [3,3], target = 6
  • Output: [0,1]

Solution (Logic)

Let's solve this question in few steps below๐ŸŽ‰

  • Set up a hash table or hash map.
    let map = new Map();
    
  • The next step is to iterate through the nums array using a for loop.
      for(let i=0; i<nums.length; i++){
      }
    
  • Set every item of the array to the map by the index.
    map.set(nums[i],i)
    
  • During iteration you will use a conditional statement to check if the value of the subtraction of the target and the current number nums[i] is in the map.
    if(map.has(target-nums[i])){
          }
    
  • The next and final step is to return the index of the number that is the result of the subtraction.
   if(map.has(target-nums[i])){
            return [map.get(target-nums[i]),i]
        }

The Compilation of the codes above is:

 let map = new Map();
    for(let i=0; i<nums.length; i++){
        map.set(nums[i],i)
        if(map.has(target-nums[i])){
            return [map.get(target-nums[i]),i]
        }
    }

It's so easy to implement ๐Ÿ˜Š

The Time and Space Complexity

Time Complexity

This Solution has a Time complexity of O(n) which is a Linear time since we are looping through the nums array and it depends on the length of the array.

Space complexity

The Space Complexity is also O(n) since we are using extra space to store the values in a map.

The Runtime is 76ms, faster than 87.35% of JavaScript online submissions for Two Sum and Memory Usage: 41.2 MB, less than 21.63% of JavaScript online submissions for Two Sum.

Two Sum Array Solution.JPG

Conclusion

I hope you have learnt a lot from this article and your contribution is also highly welcomed because we get better by learning from others, you can reach out to me on Twitter here . Watch out for more helpful contents on Data Structures and Algorithm from me.

Don't forget to Like, share and subscribe for more articles in this series called Solutions to Data Structures and Algorithm Questions on Leetcode.

Enjoy ๐ŸŽ‰