mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 07:29:47 +08:00

* fix: #758 optimised armstrongNumber code * fix:#758 Average Median code optimised * feat: TwoSum function added with test cases * revert code * Fix: #758 used ternary operator to make code more optimised * Feat: TwoSum function created with test cases * Feat: TwoSum function created with test cases * Resolved comments and changes requests
25 lines
755 B
JavaScript
25 lines
755 B
JavaScript
/**
|
|
* Given an array of integers, find two numbers that add up to a specific target.
|
|
*
|
|
* @param {number[]} nums - The array of integers.
|
|
* @param {number} target - The target sum.
|
|
* @returns {number[]} - An array containing the indices of the two numbers.
|
|
*
|
|
* @example
|
|
* const nums = [2, 7, 11, 15];
|
|
* const target = 9;
|
|
* const result = twoSum(nums, target);
|
|
* // The function should return [0, 1] because nums[0] + nums[1] = 2 + 7 = 9.
|
|
*/
|
|
|
|
const TwoSum = (nums, target) => {
|
|
const numIndicesMap = new Map()
|
|
for (let i = 0; i < nums.length; i++) {
|
|
const complement = target - nums[i]
|
|
if (numIndicesMap.has(complement)) return [numIndicesMap.get(complement), i]
|
|
numIndicesMap.set(nums[i], i)
|
|
}
|
|
return []
|
|
}
|
|
export { TwoSum }
|