-
-
Notifications
You must be signed in to change notification settings - Fork 58
London | 26-SDC-March | Zobeir Rigi | Sprint 1 | Analyse and Refactor Functions #204
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
Zobeir-Rigi
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
Zobeir-Rigi:Analyse-and-Refactor-Functions
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1258e0
Refactor calculateSumAndProduct
Zobeir-Rigi c7b1e55
explnation added
Zobeir-Rigi 0c96de0
Refactor findCommonItems using Set lookup
Zobeir-Rigi c3a6f11
Refactor hasPairWithSum using Set
Zobeir-Rigi 2a57b11
Refactor removeDuplicates using Set
Zobeir-Rigi 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,28 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n²) | ||
| * Space Complexity: O(n) | ||
| * Optimal Time Complexity: O(n + m) | ||
| * | ||
| * Original complexity: | ||
| * The original solution used filter() and includes(). | ||
| * filter() loops through every item in firstArray. | ||
| * For each item, includes() may need to search the entire | ||
| * secondArray. This results in O(n × m) time, which is | ||
| * O(n²) when both arrays are similar in size. | ||
| * | ||
| * @param {Array} firstArray - First array to compare | ||
| * @param {Array} secondArray - Second array to compare | ||
| * @returns {Array} Array containing unique common items | ||
| * Refactor: | ||
| * Convert secondArray into a Set. Set.has() provides | ||
| * constant-time lookups, so we only loop through each | ||
| * array once. This reduces the time complexity to O(n + m). | ||
| * | ||
| * Space Complexity: | ||
| * We store secondArray in a Set, which requires extra | ||
| * memory proportional to the size of secondArray. | ||
| */ | ||
| export const findCommonItems = (firstArray, secondArray) => [ | ||
| ...new Set(firstArray.filter((item) => secondArray.includes(item))), | ||
| ]; | ||
| export const findCommonItems = (firstArray, secondArray) => { | ||
| const secondSet = new Set(secondArray); | ||
|
|
||
| return [...new Set(firstArray.filter((item) => secondSet.has(item)))]; | ||
| }; | ||
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,21 +1,40 @@ | ||
| /** | ||
| * Find if there is a pair of numbers that sum to a given target value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n²) | ||
| * Space Complexity: O(1) | ||
| * Optimal Time Complexity: O(n) | ||
| * | ||
| * @param {Array<number>} numbers - Array of numbers to search through | ||
| * @param {number} target - Target sum to find | ||
| * @returns {boolean} True if pair exists, false otherwise | ||
| * Original complexity: | ||
| * The original solution uses nested loops. For each number, | ||
| * it checks every remaining number in the array looking for | ||
| * a pair whose sum equals the target. Since each number can | ||
| * be compared with many other numbers, the time complexity | ||
| * is O(n²). | ||
| * | ||
| * Refactor: | ||
| * Use a Set to store numbers that have already been seen. | ||
| * For each number, calculate the value needed to reach the | ||
| * target and check whether it exists in the Set. | ||
| * Set lookups are O(1), so we only need one pass through | ||
| * the array. | ||
| * | ||
| * Refactored Complexity: | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(n) | ||
| */ | ||
| export function hasPairWithSum(numbers, target) { | ||
| for (let i = 0; i < numbers.length; i++) { | ||
| for (let j = i + 1; j < numbers.length; j++) { | ||
| if (numbers[i] + numbers[j] === target) { | ||
| return true; | ||
| } | ||
| const seen = new Set(); | ||
|
|
||
| for (const num of numbers) { | ||
| const needed = target - num; | ||
|
|
||
| if (seen.has(needed)) { | ||
| return true; | ||
| } | ||
|
|
||
| seen.add(num); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
47 changes: 23 additions & 24 deletions
47
Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
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,36 +1,35 @@ | ||
| /** | ||
| * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n²) | ||
| * Space Complexity: O(n) | ||
| * Optimal Time Complexity: O(n) | ||
| * | ||
| * @param {Array} inputSequence - Sequence to remove duplicates from | ||
| * @returns {Array} New sequence with duplicates removed | ||
| * Original complexity: | ||
| * The original solution uses nested loops. For each item in | ||
| * the input sequence, it searches through the uniqueItems | ||
| * array to check whether the value already exists. This | ||
| * results in O(n²) time complexity in the worst case. | ||
| * | ||
| * Refactor: | ||
| * Use a Set to keep track of values that have already been | ||
| * seen. Set.has() and Set.add() are O(1) operations, allowing | ||
| * us to process the sequence in a single loop. | ||
| * | ||
| * Refactored Complexity: | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(n) | ||
| */ | ||
| export function removeDuplicates(inputSequence) { | ||
| const seen = new Set(); | ||
| const uniqueItems = []; | ||
|
|
||
| for ( | ||
| let currentIndex = 0; | ||
| currentIndex < inputSequence.length; | ||
| currentIndex++ | ||
| ) { | ||
| let isDuplicate = false; | ||
| for ( | ||
| let compareIndex = 0; | ||
| compareIndex < uniqueItems.length; | ||
| compareIndex++ | ||
| ) { | ||
| if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { | ||
| isDuplicate = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!isDuplicate) { | ||
| uniqueItems.push(inputSequence[currentIndex]); | ||
| for (const item of inputSequence) { | ||
| if (!seen.has(item)) { | ||
| seen.add(item); | ||
| uniqueItems.push(item); | ||
| } | ||
| } | ||
|
|
||
| return uniqueItems; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Could also take advantage of built-in Set operations such as union and intersect for better performance and simpler code.