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 @@ -18,17 +18,20 @@
*/
export function calculateSumAndProduct(numbers) {
let sum = 0;
for (const num of numbers) {
sum += num;
}

let product = 1;

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

}

return {
sum: sum,
product: product,
};
}

// The function has 2 loops, one for the sum and the other for the product.
// we actually don't need to loop twice, we can do both in a single loop.
// the time complexity now is 0(n) and the space complexity is O(1).
2 changes: 2 additions & 0 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
export const findCommonItems = (firstArray, secondArray) => [
...new Set(firstArray.filter((item) => secondArray.includes(item))),
];

// the function does not need to be optimized as it is already efficient.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • What's the complexity of the original code?

  • The complexity of the function could be reduced.

3 changes: 3 additions & 0 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ export function hasPairWithSum(numbers, target) {
}
return false;
}

// in this function, we need to use 2 loops to check if there is a pair of numbers that sum to the value.
// nothing can be optimized in this function, it is already efficient.
Comment on lines +22 to +24

@cjyuan cjyuan Jul 17, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • What's the complexity of the original code?

  • With a different algorithm, the complexity of the function could be reduced.

41 changes: 14 additions & 27 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
/**
* 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)
Comment on lines +4 to +6

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 indicate the time complexity of the original code?

*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @param {Array} items - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];
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 value of inputSequence) {
if (!seen.has(value)) {
seen.add(value);
uniqueItems.push(value);
}
}

return uniqueItems;
}
return uniqueItems;
}
Loading