Can someone help me in optimizing the code here?
This is the original question
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Question link: https://leetcode.com/problems/two-sum/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
const hashMapArray = {}
for (let i=0; i<nums.length; i++) {
const num = nums[i]
for (let index in hashMapArray) {
if (hashMapArray[index] + num === target) {
return [index, i]
}
}
hashMapArray[i] = num
}
return []
}
Frankly, I was expecting my code to be optimized but then this what I got as a result and I was kinda heartbroken
How can I make it better?