DEV Community

Cover image for 1. Two Sum - Leeetcode - JavaScript Solution using for loops - by Abu Saleh Faysal
Abu Saleh Faysal
Abu Saleh Faysal

Posted on • Updated on

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

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

Image description

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

Top comments (0)