-
-
Notifications
You must be signed in to change notification settings - Fork 45
London | 26-SDC-Mar | beko | Sprint 1 | analyse and refactor functions #173
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
Abubakar-Meigag
wants to merge
1
commit into
CodeYourFuture:main
Choose a base branch
from
Abubakar-Meigag:beko-complexity-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 all commits
Commits
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,17 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: now is O(n^2) - coz we doing instead loop one with the filter first array then we checking the array with includes loop to find out if the item are common in both arrays | ||
| * Space Complexity: is O(n) - coz it is grows directly proportional with the input size | ||
| * Optimal Time Complexity: to O(n), after i used a Set, now the lookup is faster instead of checking the whole second array each time | ||
| * | ||
| * @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) => { | ||
| let second = new Set(secondArray) | ||
| return [ ...new Set(firstArray.filter((item) => second.has(item))) ] | ||
| } | ||
| console.log(findCommonItems([1, 2, 2, 3], [4, 3, 2])) | ||
|
|
||
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,27 @@ | ||
| /** | ||
| * Find if there is a pair of numbers that sum to a given target value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: is O(n^2) coz we have nested loop in the function | ||
| * Space Complexity: is O(n) coz it grows proportionally with the input size | ||
| * Optimal Time Complexity: to O(n) - and that by using Set as check point to fast lookup and catch up if it has the number instead of re-scanning everything | ||
| * | ||
| * @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 | ||
| */ | ||
| 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 checkPoint = new Set() | ||
|
|
||
| for(let i=0; i < numbers.length; i++){ | ||
| let num = target - numbers[i] | ||
| if(checkPoint.has(num)){ | ||
| return true | ||
| }else { | ||
| checkPoint.add(numbers[i]) | ||
|
Comment on lines
+15
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The formatting of the code is not quite consistent. More importantly, have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode? |
||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| console.log(hasPairWithSum([1, 2, 4, 5, 3], 9)); | ||
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,18 @@ | ||
| /** | ||
| * 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: is O(n^2) coz it used nested loop to remove the duplications | ||
| * Space Complexity: it is O(n) coz it is grows directly proportional with the input size | ||
| * Optimal Time Complexity: to O(n) - because even if i'm using Set, i need to go through each element in the array, which leads me to the time complexity will be growing directly proportional with the input size | ||
| * | ||
| * @param {Array} inputSequence - Sequence to remove duplicates from | ||
| * @returns {Array} New sequence with duplicates removed | ||
| */ | ||
| export function removeDuplicates(inputSequence) { | ||
| 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]); | ||
| } | ||
| } | ||
| const uniqueItems = [...new Set(inputSequence)] | ||
|
|
||
| return uniqueItems; | ||
| } | ||
|
|
||
| console.log(removeDuplicates([5, 2, 2, 3, 4, 4, 1])) | ||
| console.log(removeDuplicates(["a", "e", "b", "a", "c", "c", "a"])) |
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.
Note: A set data structure typically supports set operations such as union, intersect, etc. You could also explore using these methods to simplify your solution.