Skip to content

London | 25-SDC-Nov | Emiliano Uruena | Sprint 1 | Analyse and Refactor Functions#129

Open
Emilianouz wants to merge 6 commits intoCodeYourFuture:mainfrom
Emilianouz:Sprint1
Open

London | 25-SDC-Nov | Emiliano Uruena | Sprint 1 | Analyse and Refactor Functions#129
Emilianouz wants to merge 6 commits intoCodeYourFuture:mainfrom
Emilianouz:Sprint1

Conversation

@Emilianouz
Copy link

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

Refactor algorithms for improved efficiency and Complexity.

@Emilianouz Emilianouz 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 Feb 27, 2026
* Optimal Time Complexity:
* Time Complexity: for each element in the input is compared against all previous unique elements: o(n2)
* Space Complexity: we store n elements it all elements are unique.
* Optimal Time Complexity: Each element is processed once O(1).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate how you derived the optimal time complexity of O(1)?

Comment on lines +16 to +18
let i = 0;
i < inputSequence.length;
i++
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 a for-of loop?

Comment on lines 22 to 25
if (!seenItems.has(value)) {
seenItems.add(value);
uniqueItems.push(value);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS Set object preserves the order of which the elements are added to the set. So for better performance we could rely on its built-in ability to convert an array to a set and vice versa.

Space Complexity:
space is constant O(1)
Optimal time complexity:
With only one loop for both operations reduce complexity to o(1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optimal time complexity refers to the complexity of the function, and it is not O(1).

Comment on lines +12 to +14
Time Complexity: for each element in first_sequence, is compared against every element in second_sequence.
Space Complexity: O(the number of unique common elements)
Optimal time complexity: improved using a set for fast lookups
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you specify both complexities in big-O notations?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 2, 2026
Updated the optimal time complexity comment to reflect correct analysis.
Clarified optimal time complexity explanation in comments.
Clarified optimal time and space complexity in docstring.
Updated time and space complexity explanations in docstring.
@Emilianouz Emilianouz added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Mar 3, 2026
}
Time Complexity:
There were two loops, one for each operation. Each has complexity O(n)
for both loops, complexity is O(2n)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Constant multiplier is ignored in complexity analysis. O(2n) and O(n) means the same thing.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: The time complexity is O(n + m) because we build a set from the second sequence in O(m) time and iterate through the first sequence in O(n) time.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a complexity analysis for the refactored code. The original code does not use any set.

What's the complexity of the original code?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 1 Assigned during Sprint 1 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants