-
-
Notifications
You must be signed in to change notification settings - Fork 29
London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 1 | Analyse and refactor functions #141
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 8 commits
c0f3080
5c99d88
4530871
810820e
f396f7a
12f0bee
b5e1e07
b7a255f
779628c
18ef494
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,36 +1,29 @@ | ||
| /** | ||
| * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
| * | ||
| * Time Complexity: | ||
| * Time Complexity: | ||
| * ANSWER:O(n) | ||
| Because the array is looped through once. | ||
| * Space Complexity: | ||
| ANSWER:O(n) | ||
| Because a Set is used to track seen items. | ||
| * Optimal Time Complexity: | ||
| ANSWER:O(n) | ||
| Each item is processed once using a Set for lookup. | ||
| * | ||
| * @param {Array} inputSequence - Sequence to remove duplicates from | ||
| * @returns {Array} New sequence with duplicates removed | ||
| */ | ||
| export function removeDuplicates(inputSequence) { | ||
| const uniqueItems = []; | ||
| const seen = new Set(); | ||
| const result = []; | ||
|
|
||
| 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 item of inputSequence) { | ||
| if (!seen.has(item)) { | ||
| seen.add(item); | ||
| result.push(item); | ||
| } | ||
| } | ||
|
|
||
| return uniqueItems; | ||
| return result; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,10 @@ def find_common_items( | |
| Optimal time complexity: | ||
| """ | ||
| common_items: List[ItemType] = [] | ||
| second_sequence_set = set(second_sequence) | ||
|
|
||
| for i in first_sequence: | ||
| for j in second_sequence: | ||
| if i == j and i not in common_items: | ||
| common_items.append(i) | ||
| return common_items | ||
| if i in second_sequence_set and i not in common_items: | ||
| common_items.append(i) | ||
|
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. Can also consider using a built-in set operation for better performance and simpler 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. Thanks for your feedback @cjyuan !I’ve made the suggested changes, including using the built-in set intersection and updating the complexity comments. Please let me know if anything else needs improvement. 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. The new implementation looks good. |
||
|
|
||
| return common_items | ||
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.
I think "Time Complexity" here refers to the time complexity of the original implementation, and "Optimal Time Complexity" refers to the time complexity of the refactored version. (Otherwise they will always have the same complexity)