-
-
Notifications
You must be signed in to change notification settings - Fork 14
London | 25-SDC-July | Fatma Arslantas | Sprint 1 | Analyse and Refactor Functions #73
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
base: main
Are you sure you want to change the base?
Changes from all commits
df10604
22d24ff
ca36a81
eab0a3c
0e50700
1565415
d56406c
31c81a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,31 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n + m) | ||
| * The old code used .includes() inside .filter(), which meant a loop inside a loop (O(n^2)). | ||
| * By using a 'Set', we can check if an item exists instantly (O(1)). | ||
| * | ||
| * Space Complexity: O(n) - We create a Set to store the items from the first array. | ||
| * | ||
| * Optimal Time Complexity: O(n) - We must look at every item at least once to compare them. | ||
| * | ||
| * @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) => { | ||
| // Refactor: I put the first array into a Set for fast checking. | ||
| const itemsInFirst = new Set(firstArray); | ||
| const commonItems = new Set(); | ||
|
|
||
| for (const item of secondArray) { | ||
| // Checking 'itemsInFirst.has(item)' is very fast O(1). | ||
| // The old 'includes()' was slow because it searched the whole list O(n). | ||
| if (itemsInFirst.has(item)) { | ||
| commonItems.add(item); | ||
|
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. While the complexity remains unchanged, appending an item to an array is typically faster than adding an item to a set. |
||
| } | ||
| } | ||
|
|
||
| // Convert the Set back to an array to return it | ||
| return [...commonItems]; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,35 @@ | ||
| /** | ||
| * 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) | ||
| * The old code used nested loops (loop inside a loop) which is slow O(n^2). | ||
| * I used a Set to remember the numbers we have seen. This lets us find the answer in one loop O(n). | ||
| * | ||
| * Space Complexity: O(n) - We use a Set to store the numbers we have visited. | ||
| * Optimal Time Complexity: O(n) - We need to check the numbers at least once to find the pair. | ||
| * | ||
| * @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; | ||
| } | ||
| // Refactor: I used a Set to store numbers we have already seen. | ||
| const seenNumbers = new Set(); | ||
|
|
||
| for (const num of numbers) { | ||
| // Calculate what number we need to reach the target. | ||
| // Example: If target is 10 and num is 3, we need 7. | ||
| const match = target - num; | ||
|
|
||
| // Check if the number we need is already in our Set. | ||
| // .has() is very fast O(1). | ||
| if (seenNumbers.has(match)) { | ||
| return true; | ||
| } | ||
|
|
||
| // Add the current number to the Set so we can use it later. | ||
| seenNumbers.add(num); | ||
| } | ||
|
|
||
| return false; | ||
| } |
| 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: O(n) | ||
| * The old code used a nested loop (checking the result array for every item), which is O(n^2). | ||
| * A Set automatically removes duplicates in one go. Converting Array -> Set -> Array takes linear time O(n). | ||
| * | ||
| * Space Complexity: O(n) - We create a new Set/Array to store the unique items. | ||
| * Optimal Time Complexity: O(n) - We must look at every item at least once to see what it is. | ||
| * | ||
| * @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]); | ||
| } | ||
| } | ||
|
|
||
| return uniqueItems; | ||
| // Refactor: I used a Set to remove duplicates automatically. | ||
| // It creates a Set (removes duplicates) and spreads it back into an array. | ||
| return [...new Set(inputSequence)]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,19 +7,24 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: | |
| """ | ||
| 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) | ||
| The old code checked the list repeatedly, which is O(n^2). | ||
| I used a 'Set' to remember seen items for O(1) lookups, while appending to a list to keep the order. | ||
|
|
||
| Space Complexity: O(n) - We use a Set to store seen items and a List for the result. | ||
|
|
||
| Optimal time complexity: O(n) - We must iterate through the input at least once. | ||
| """ | ||
|
|
||
| # Refactor: I used a Set to track items we have seen because checking a Set is O(1). | ||
| # I also used a List to build the result so we keep the original order. | ||
| seen = set() | ||
| unique_items = [] | ||
|
|
||
| for value in values: | ||
| is_duplicate = False | ||
| for existing in unique_items: | ||
| if value == existing: | ||
| is_duplicate = True | ||
| break | ||
| if not is_duplicate: | ||
| # Check if we have seen this value before. | ||
| if value not in seen: | ||
| unique_items.append(value) | ||
| seen.add(value) | ||
|
|
||
| return unique_items | ||
|
Comment on lines
+21
to
30
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. Why not use the approach you used in the JS version? |
||
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.
Even though the performance is improved by combining the loops, the
complexity remains O(n); the constant factor is ignored in complexity analysis.