[1. Two Sum] #tags: leetcode
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
const map = {};
let result = [];
for (let i = 0; i < nums.length; ++i) {
map[nums[i]] = i;
}
for (let i = 0; i < nums.length; ++i) {
const other = target- nums[i];
if (map[other] !== undefined && map[other] !== i) {
result = [i, map[other]];
break;
}
}
return result;
};