Skip to content

Conversation

@AFatmaa
Copy link

@AFatmaa AFatmaa commented Jan 16, 2026

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

In this PR, I analysed and refactored the functions in Sprint 1 to improve their performance.

  • I identified inefficient code (mostly nested loops with $O(n^2)$ complexity).
  • I refactored the solutions using Set and single loops to reduce complexity to $O(n)$.
  • I added comments to explain the Time and Space Complexity for each function.
  • I applied these changes to both JavaScript and Python files.

Questions

I don’t have any questions thank you!

@AFatmaa AFatmaa added 📅 Sprint 1 Assigned during Sprint 1 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jan 16, 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).
Copy link

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);
Copy link

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.

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
Copy link

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?

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jan 23, 2026
@cjyuan
Copy link

cjyuan commented Jan 23, 2026

Complexity analysis is correct and clearly explained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. 📅 Sprint 1 Assigned during Sprint 1 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants