From f620b8cc74eee2ceb760a67baa070ad982dbacf4 Mon Sep 17 00:00:00 2001 From: Craig D'Silva Date: Tue, 16 Jun 2026 19:20:30 +0100 Subject: [PATCH 1/4] calculateSumAndProduct --- .../calculateSumAndProduct/calculateSumAndProduct.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..9fdfdfc 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -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; } @@ -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. \ No newline at end of file From 345b688677a048f5e4e0be587e164331a147ba03 Mon Sep 17 00:00:00 2001 From: Craig D'Silva Date: Tue, 16 Jun 2026 20:14:50 +0100 Subject: [PATCH 2/4] findCommonItems --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..4a682fd 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -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 \ No newline at end of file From 50abf7afac351dd632097f913928857f01057838 Mon Sep 17 00:00:00 2001 From: Craig D'Silva Date: Fri, 19 Jun 2026 09:10:40 +0100 Subject: [PATCH 3/4] hasPairWithSum --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..c7001aa 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -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. \ No newline at end of file From 48e1bb38a93ac91d65d0a033f886c23d14bb516e Mon Sep 17 00:00:00 2001 From: Craig D'Silva Date: Fri, 19 Jun 2026 09:29:00 +0100 Subject: [PATCH 4/4] removeDuplicates --- .../removeDuplicates/removeDuplicates.mjs | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..69f76e7 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -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. \ No newline at end of file