Skip to content

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 1 | Analyse and refactor functions#141

Open
aydaeslami wants to merge 8 commits intoCodeYourFuture:mainfrom
aydaeslami:Analyse-and-Refactor-Functions
Open

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 1 | Analyse and refactor functions#141
aydaeslami wants to merge 8 commits intoCodeYourFuture:mainfrom
aydaeslami:Analyse-and-Refactor-Functions

Conversation

@aydaeslami
Copy link

Self checklist

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

Changelist

Refactored functions to reduce time complexity by removing nested loops and using sets for faster lookups, improving overall performance.

@aydaeslami aydaeslami 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 Mar 1, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

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

Code is good.

Can you also show the time complexity of the original implementation?

Comment on lines 4 to +7
* Time Complexity:
* Space Complexity:
* ANSWER: O(n + m)
Because the second array is converted into a Set once, and the first array is looped through once.

Copy link

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)

export const findCommonItems = (firstArray, secondArray) => {
const secondSet = new Set(secondArray);

return [...new Set(firstArray.filter(item => secondSet.has(item)))];
Copy link

Choose a reason for hiding this comment

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

Note: Set object has a built-in method that can be used to find the "common items" between two sets.
Using built-in method usually means better performance and shorter code.

Comment on lines +20 to +21
if i in second_sequence_set and i not in common_items:
common_items.append(i)
Copy link

Choose a reason for hiding this comment

The 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.

@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
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