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

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

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

// This function had 2 loops so the time complexity was O(n2).
// The updated code has time complexity of O(n) since there is only 1 loop.
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))),
];

// We can't improve this as we need a nested loops are necessary for comparison
2 changes: 2 additions & 0 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export function hasPairWithSum(numbers, target) {
}
return false;
}

// In order to find the pair, it needs to do 2 loops. Nothing much can be done here to improve efficiency.
28 changes: 4 additions & 24 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,8 @@
* @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;
return [...new Set(inputSequence)];
}

// The previous code had a time complexity of O(n2) because it had 2 loops to check for duplicates.
// Set has time complexity of O(n) as it just deletes duplicates when needed and the time complexity depends on the input size.
Loading