Skip to main content

Command Palette

Search for a command to run...

1. Two Sum — Leetcode — JavaScript Solution using for loops - by Abu Saleh Faysal

Updated
2 min read
1. Two Sum — Leetcode — JavaScript Solution using for loops - by Abu Saleh Faysal
A

Experienced software engineer with a demonstrated history of working in several tech startups. Skilled in Front-End Development using Bootstrap, TailwindCSS, JavaScript, TypeScript, React, Redux, Firebase. Strong software professional with a Bachelor of Science focused in Computer Science and Engineering from BRAC University.

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:

tt4p5qome1ic6atin400.png

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.

Video Tutorial

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: https://www.linkedin.com/company/thebacklogprogrammer/

👉 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

More from this blog

Abu Saleh Faysal's blog

6 posts