Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity:O(n)
* Space Complexity: O(1)
* Optimal Time Complexity:O(n)
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
*/

// The refactor code ....
export function calculateSumAndProduct(numbers) {
let sum = 0;
let product =1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spacing around = is inconsistent.

for (const num of numbers) {
sum += num;
}

let product = 1;
for (const num of numbers) {
product *= num;
}

Expand All @@ -32,3 +31,5 @@ export function calculateSumAndProduct(numbers) {
product: product,
};
}

// The refactor code is better because it combine two separate loop into one and calculate the sum & Product.
24 changes: 18 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n2)
* Space Complexity: O(n)
* 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))),
];

//refactor code
export function findCommonItems (firstArray, secondArray){

const firstSet = new Set(firstArray);
const secondSet = new Set(secondArray);

const commonSet = firstSet.intersection(secondSet);


return [...commonSet];
}


// This refactor code is better because it avoid to check every single item from two arrays for filter items & searching that each items includes in the second array .

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your complexity analysis is correct if the length of both arrays are $n$. What if their lengths are $n$ and $m$ respectively?

Also, your explanation does not quite explain how you derived the optimal complexity. Could you elaborate?

32 changes: 29 additions & 3 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* 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(n2)
* 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
*/

/*
export function hasPairWithSum(numbers, target) {
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
Expand All @@ -19,3 +21,27 @@ export function hasPairWithSum(numbers, target) {
}
return false;
}
*/

//refactor code

export function hasPairWithSum(numbers,target){
const seen = new Set();

for (const num of numbers)
{
const needed = target-num;
if (seen.has(needed))
{
return true;
}
else
{
seen.add(num);
}
}

}
Comment on lines +28 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • Why not use VS Code's "Format document" feature or any other formatting tool to keep the code consistently formatted?

  • The refactored code has a bug. Please fix it.



// The refactor code is better because it avoid nested loop but it create set so space complexity will be o(n).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Saying "avoid nested loops" does not fully explain why the refactored implementation is better than the original. Instead, you could compare their time complexities to clearly explain the performance improvement.

On separate note, could you elaborate how you determined the complexity of the refactored implementation is $O(n)$?

20 changes: 17 additions & 3 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* 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(n2)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n)
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/

/*
export function removeDuplicates(inputSequence) {
const uniqueItems = [];

Expand All @@ -34,3 +36,15 @@ export function removeDuplicates(inputSequence) {

return uniqueItems;
}
*/

//refactor code
export function removeDuplicates(inputSequence)
{
const outputSequence = [...new Set(inputSequence)];
return outputSequence;
}


//The refactor code is better because it avoid the nested for loop operation ....

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you also rephrase this statement to explain how you determine the complexity of the refactored code is $O(n)$?


Loading