Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
Feat: TwoSum function created with test cases#1399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
4e38ab5
fix: #758 optimised armstrongNumber code
developerr-ayushf8f0e2a
Merge branch 'master' of https://github.com/developerr-ayush/JavaScript
developerr-ayush184ae84
fix:#758 Average Median code optimised
developerr-ayushb8be2f9
feat: TwoSum function added with test cases
developerr-ayush882db43
revert code
developerr-ayushc58f28f
Fix: #758 used ternary operator to make code more optimised
developerr-ayushbdf8ed8
Feat: TwoSum function created with test cases
developerr-ayush8bde9b9
Feat: TwoSum function created with test cases
developerr-ayusha254b86
Merge branch 'TheAlgorithms:master' into master
developerr-ayush97e147c
Merge branch 'TheAlgorithms:master' into Feature-TwoSum-function
developerr-ayush01539b5
Merge pull request #1 from developerr-ayush/Feature-TwoSum-function
developerr-ayush5733b78
Resolved comments and changes requests
developerr-ayushFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { TwoSum } from '../TwoSum.js' | ||
describe('Two Sum', () => { | ||
const testCasesWithoutSolution = [ | ||
[[8], 8], | ||
[[3, 3, 3, 3], 19] | ||
] | ||
const testCasesWithSolution = [ | ||
[[2, 7, 11, 15], 9, [0, 1]], | ||
[[15, 2, 11, 7], 13, [1, 2]], | ||
[[2, 7, 11, 15], 17, [0, 3]], | ||
[[7, 15, 11, 2], 18, [0, 2]], | ||
[[2, 7, 11, 15], 26, [2, 3]] | ||
] | ||
test.each(testCasesWithoutSolution)( | ||
'Should return an empty array if there is no solution', | ||
(nums, target) => { | ||
expect(TwoSum(nums, target)).toEqual([]) | ||
} | ||
) | ||
test.each(testCasesWithSolution)( | ||
'Should return the indices of two numbers that add up to the target', | ||
(nums, target, expected) => { | ||
expect(TwoSum(nums, target)).toEqual(expected) | ||
} | ||
) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.