-
-
Notifications
You must be signed in to change notification settings - Fork 29
London | 25-SDC-July | Andrei Filippov | Sprint 1 | Analyse and Refactor Functions #27
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
Open
Droid-An
wants to merge
4
commits into
CodeYourFuture:main
Choose a base branch
from
Droid-An:Sprint-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,29 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n*m) or O(n^2) where n and m are length of each array | ||
| * Space Complexity: O(n) where n is a length of arrays | ||
| * Optimal Time Complexity: O(n) | ||
| * | ||
| * @param {Array} firstArray - First array to compare | ||
| * @param {Array} secondArray - Second array to compare | ||
| * @returns {Array} Array containing unique common items | ||
| */ | ||
| export const findCommonItems = (firstArray, secondArray) => [ | ||
| ...new Set(firstArray.filter((item) => secondArray.includes(item))), | ||
| ]; | ||
| export const findCommonItems = (firstArray, secondArray) => { | ||
| // [...new Set(firstArray.filter((item) => secondArray.includes(item))),] | ||
| // program flow: | ||
| // firstArray.filter go through the each item in first array (loop) | ||
| // secondArray.includes check if that item is in the second array (nested loop) | ||
| // then, from new filtered array we create a Set which is not a nested array, but still O(n) operation | ||
| // nested loop result in O(n^2) time complexity | ||
|
|
||
| // to reduce time complexity to sublinear on the number of elements in the arrays we can use Set and it's .intersection() method | ||
|
|
||
| // complexity of this operation is O(n) | ||
| const firstArraySet = new Set(firstArray); | ||
| // complexity of this operation is O(m) | ||
| const secondArraySet = new Set(secondArray); | ||
| // intersection method uses has method which is considered to be faster than O(n) and since JS uses hash tables for has methods, the complexity should be | ||
| // has O(1) and intersection O(n) which result in O(n) overall time complexity | ||
| return Array.from(firstArraySet.intersection(secondArraySet)); | ||
| }; |
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
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
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.
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.
Comparison-based sorting algorithm is at best O(nlogn).
Note: Without sorting, an O(n) approach involving map/dict is still possible.
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.
Hi, thank you for the review. I didn’t know that the sorted() function has O(n log n) time complexity. After googling it, I learned that this is because sorted() uses the Timsort algorithm.
I followed your suggestion and found a better algorithm to find sum in O(n) time by using set.