Two Sum - Leetcode 1 - HashMap - Javascript

Published: 21 December 2025
on channel: CodeWithYash
54
2

🔔 Two Sum (LeetCode #1) – Optimized Solution | HashMap Approach
📌 LeetCode: https://leetcode.com/problems/two-sum/

In this video, we solve one of the most popular coding interview problems — Two Sum — using the optimized HashMap solution! 💡
This approach drastically improves performance compared to brute force and is ideal for interview prep.

🧠 Problem Overview
You’re given an integer array nums and a target value.
Find indices of the two numbers such that they add up to the target.
⚠️ You can assume exactly one solution exists.
Example:
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]

🚀 Optimized Approach — HashMap (One Pass)
✔️ Use a dictionary (HashMap) to store numbers and their indices
✔️ For each number, check if the complement (target - num) is already in the map
✔️ If yes → return indices
✔️ Else → save number & index in map
📈 Time Complexity: O(n)
📦 Space Complexity: O(n)

🔍 What You’ll Learn in This Video
✨ How to use a HashMap for faster lookups
✨ Why this approach is better than brute force
✨ Example walkthrough with an array
✨ Perfect for interview and competitive coding

💬 Comment Below
Want this solution in another language (Java / C++ / JavaScript)? Let me know! 👇

📌 Subscribe for more LeetCode solutions & interview prep content!
✅ Algorithms | Python | Java | JavaScript | Coding Challenges


On this page of the site you can watch the video online Two Sum - Leetcode 1 - HashMap - Javascript with a duration of hours minute second in good quality, which was uploaded by the user CodeWithYash 21 December 2025, share the link with friends and acquaintances, this video has already been watched 54 times on youtube and it was liked by 2 viewers. Enjoy your viewing!