1. Two Sum — Leeetcode — JavaScript Solution

Abu Saleh Faysal
2 min readNov 10, 2022

1. Two Sum

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
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Solution

Step 01: I simply iterate the array using for loops, and inside the for loops I iterate the array again but this time start the index with the next position(compare to the first iteration)
Step 02: In this way, I got pair of numbers from the array and check if the total of the pair is equal to the target number or not.
Step 03: If the total value is equal to the target, I simply return the index numbers.

If you want me to publish more posts like this, Buy me a coffee.

👉 YouTube Channel Link: https://www.youtube.com/channel/UCW_09Nbobf4URLkAlEo84sw
👉 PlayList Link: https://youtube.com/playlist?list=PLUnklBXn8NSefCpBaLe39mds6dQx-tDDD

👉 Connect with me (LinkedIn): https://www.linkedin.com/in/abusalehfaysal
👉 Follow our LinkedIn Page:
👉 Like our Facebook page: https://www.facebook.com/thebacklogprogrammer/
👉 Join our community (Facebook group): https://www.facebook.com/groups/5500588936676942/
👉 Follow me at: https://www.facebook.com/AbuSalehFaysal10
👉 Twitter: https://twitter.com/AbuSalehFaysal

👉 Abu Saleh Faysal’s Blog: https://abusalehfaysal.hashnode.dev/
👉 Hasnode: https://hashnode.com/@AbuSalehFaysal
👉 Dev Community: https://dev.to/abusalehfaysal
👉 freeCodeCamp: https://www.freecodecamp.org/abusalehfaysal
👉 Medium: https://abusalehfaysal.medium.com/

👉 GitHub: https://github.com/AbuSalehFaysal
👉 GitLab: https://gitlab.com/AbuSalehFaysal

--

--

Abu Saleh Faysal

Software Engineer at Onethread | Front-End Developer | Blockchain Enthusiast | JavaScript | TypeScript | React | Redux