-
-
Notifications
You must be signed in to change notification settings - Fork 29
Manchester|25-SDC-NOV| Rahwa Haile | Sprint 1|Analyse and refactor #109
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
ff591c0
6c5bcf5
45299ad
7e808a4
069d331
88fbf86
38e928f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n + m) | ||
| * Space Complexity: O(n + m) | ||
| * Optimal Time Complexity: O(n + m) | ||
| * | ||
| * @param {Array} firstArray - First array to compare | ||
| * @param {Array} secondArray - Second array to compare | ||
| * @returns {Array} Array containing unique common items | ||
| */ | ||
| export const findCommonItems = (firstArray, secondArray) => [ | ||
| ...new Set(firstArray.filter((item) => secondArray.includes(item))), | ||
| ]; | ||
| export const findCommonItems = (firstArray, secondArray) => { | ||
| const secondSet = new Set(secondArray); | ||
| const seen = new Set(); | ||
| const result = []; | ||
|
|
||
| for (const item of firstArray) { | ||
| if (secondSet.has(item) && !seen.has(item)) { | ||
| seen.add(item); | ||
| result.push(item); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,23 @@ | ||
| /** | ||
| * Find if there is a pair of numbers that sum to a given target value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(n) | ||
| * Optimal Time Complexity: O(n) | ||
| * | ||
| * @param {Array<number>} numbers - Array of numbers to search through | ||
| * @param {number} target - Target sum to find | ||
| * @returns {boolean} True if pair exists, false otherwise | ||
| */ | ||
| export function hasPairWithSum(numbers, target) { | ||
| for (let i = 0; i < numbers.length; i++) { | ||
| for (let j = i + 1; j < numbers.length; j++) { | ||
| if (numbers[i] + numbers[j] === target) { | ||
| return true; | ||
| } | ||
| const seen = new Set(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of set |
||
|
|
||
| for (const num of numbers) { | ||
| if (seen.has(target - num)) { | ||
| return true; | ||
| } | ||
| seen.add(num); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this file is not needed here |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initial time complexity is worse than O(n)