Skip to main content

Command Palette

Search for a command to run...

Two Sum in JavaScript

Published
3 min read
Two Sum in JavaScript
A

Olamilekan is a software engineer with 2+ years of professional experience in the tech industry, I have a strong background in Web development, working with technologies such as HTML, CSS, Tailwind, JavaScript, TypeScript, React, Redux, Next.js, Node.js, MongoDB etc

In addition to my technical skills, I also have experience in technical writing and I am able to clearly communicate complex technical concepts to both technical and non-technical audiences.

I am constantly keeping up with the latest industry developments and I am always eager to learn new technologies.

When I am not coding I spend time on YouTube watching videos about different Gadgets, Workspace set up, Cars, Smart Homes and many other interesting areas of life 😁

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 🎉

More from this blog

Arimoro Olamilekan Ezekiel's Blog

23 posts

I am a solution driven Software developer.