-
-
Notifications
You must be signed in to change notification settings - Fork 27
Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 1 | Analyse and Refactor Functions #122
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 all commits
ddde162
01895f9
8fafe7b
45371a3
450ca3e
2ca3122
a29e73e
1344256
58344c5
9b853cc
67f8aab
c778939
2916a36
6b1203e
d9501bc
7beaf4a
ac594c9
c757b24
2d34b6d
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,32 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n * m) - filter checks each element of firstArray and includes scans secondArray | ||
| * Space Complexity: O(n) - filter creates a new array where size grows as the input of matches grows. Same thing with set. | ||
| * Optimal Time Complexity: O(n + m) - each element must be examined at least once; using a Set allows O(1) lookups | ||
| * | ||
| * @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) => [ | ||
| // ...new Set(firstArray.filter((item) => secondArray.includes(item))), | ||
| // ]; | ||
|
|
||
|
|
||
|
|
||
| export const findCommonItems = (firstArray, secondArray) => { | ||
| const arrayToSet = new Set(secondArray); | ||
| const uniqueCommonItems = []; | ||
| const checkedItems = new Set(); | ||
|
|
||
| for (const element of firstArray) { | ||
| if (arrayToSet.has(element) && !checkedItems.has(element)) { | ||
| checkedItems.add(element); | ||
| uniqueCommonItems.push(element); | ||
| } | ||
|
|
||
| } | ||
| return uniqueCommonItems; | ||
|
Comment on lines
+20
to
+31
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. This work but code can be further simplified. Optional challenge: Replace line 20-31 by one line of code.
Author
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. Ok. I will look into it once I am done with the rest of backlog. Thank you for the feedback CJ :) |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,38 @@ | ||
| /** | ||
| * 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²) - since it's a nested loop over the same array. | ||
| * Space Complexity: O(1) - since there's no other array, set, object that gorws proportionally to the input size, space usage doesn't grow. | ||
| * Optimal Time Complexity: O(n) - each number is checked at least once after refactoring. | ||
| * | ||
| * @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; | ||
| // } | ||
| // } | ||
| // } | ||
| // return false; | ||
| // } | ||
|
|
||
|
|
||
|
|
||
| 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; | ||
| } | ||
| let checkedNumbers = new Set(); | ||
|
|
||
| for (const number of numbers) { | ||
| if (checkedNumbers.has(target - number)) { | ||
| return true; | ||
| } | ||
| checkedNumbers.add(number); | ||
| } | ||
| return false; | ||
|
|
||
| } | ||
|
|
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.
In complexity analysis, we ignore the constant multiplier; O(2n) is treated the same as O(n).
Nevertheless, it is true that performance can usually be improved by reducing number of loops,