London | 25-SDC-July | Fatma Arslantas | Sprint 1 | Analyse and Refactor Functions#73
Open
AFatmaa wants to merge 8 commits intoCodeYourFuture:mainfrom
Open
London | 25-SDC-July | Fatma Arslantas | Sprint 1 | Analyse and Refactor Functions#73AFatmaa wants to merge 8 commits intoCodeYourFuture:mainfrom
AFatmaa wants to merge 8 commits intoCodeYourFuture:mainfrom
Conversation
3 tasks
cjyuan
reviewed
Jan 23, 2026
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n) - We go through the list only once. The previous code had two loops (2n), but now we do everything in one loop (n). |
There was a problem hiding this comment.
Even though the performance is improved by combining the loops, the
complexity remains O(n); the constant factor is ignored in complexity analysis.
| // Checking 'itemsInFirst.has(item)' is very fast O(1). | ||
| // The old 'includes()' was slow because it searched the whole list O(n). | ||
| if (itemsInFirst.has(item)) { | ||
| commonItems.add(item); |
There was a problem hiding this comment.
While the complexity remains unchanged, appending an item to an array is typically faster than adding an item to a set.
Comment on lines
+21
to
30
| seen = set() | ||
| unique_items = [] | ||
|
|
||
| for value in values: | ||
| is_duplicate = False | ||
| for existing in unique_items: | ||
| if value == existing: | ||
| is_duplicate = True | ||
| break | ||
| if not is_duplicate: | ||
| # Check if we have seen this value before. | ||
| if value not in seen: | ||
| unique_items.append(value) | ||
| seen.add(value) | ||
|
|
||
| return unique_items |
There was a problem hiding this comment.
Why not use the approach you used in the JS version?
|
Complexity analysis is correct and clearly explained. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Learners, PR Template
Self checklist
Changelist
In this PR, I analysed and refactored the functions in Sprint 1 to improve their performance.
Questions
I don’t have any questions thank you!