-
-
Notifications
You must be signed in to change notification settings - Fork 58
Glasgow | 26-SDC-Mar | Mohammed Abdoon | Sprint 1 | Analyse and Refactor Functions #215
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
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 |
|---|---|---|
|
|
@@ -19,3 +19,6 @@ export function hasPairWithSum(numbers, target) { | |
| } | ||
| return false; | ||
| } | ||
|
|
||
| // in this function, we need to use 2 loops to check if there is a pair of numbers that sum to the value. | ||
| // nothing can be optimized in this function, it is already efficient. | ||
|
Comment on lines
+22
to
+24
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.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,23 @@ | ||
| /** | ||
| * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(n) | ||
| * Optimal Time Complexity: O(n) | ||
|
Comment on lines
+4
to
+6
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. Could you also indicate the time complexity of the original code? |
||
| * | ||
| * @param {Array} inputSequence - Sequence to remove duplicates from | ||
| * @param {Array} items - Sequence to remove duplicates from | ||
| * @returns {Array} New sequence with duplicates removed | ||
| */ | ||
| export function removeDuplicates(inputSequence) { | ||
| const uniqueItems = []; | ||
| const seen = new Set(); | ||
| const uniqueItems = []; | ||
|
|
||
| for ( | ||
| let currentIndex = 0; | ||
| currentIndex < inputSequence.length; | ||
| currentIndex++ | ||
| ) { | ||
| let isDuplicate = false; | ||
| for ( | ||
| let compareIndex = 0; | ||
| compareIndex < uniqueItems.length; | ||
| compareIndex++ | ||
| ) { | ||
| if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { | ||
| isDuplicate = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!isDuplicate) { | ||
| uniqueItems.push(inputSequence[currentIndex]); | ||
| } | ||
| } | ||
| for (const value of inputSequence) { | ||
| if (!seen.has(value)) { | ||
| seen.add(value); | ||
| uniqueItems.push(value); | ||
| } | ||
| } | ||
|
|
||
| return uniqueItems; | ||
| } | ||
| return uniqueItems; | ||
| } | ||
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.
What's the complexity of the original code?
The complexity of the function could be reduced.