-
-
Notifications
You must be signed in to change notification settings - Fork 14
London | 25-SDC-July | Fatma Arslantas | Sprint 1 | Analyse and Refactor Functions #73
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?
Conversation
| * 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the complexity remains unchanged, appending an item to an array is typically faster than adding an item to a set.
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the approach you used in the JS version?
|
Complexity analysis is correct and clearly explained. |
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!