diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..12e1252 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,26 +9,45 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) original has two passes, optimised uses one pass + * Space Complexity: O(1) for both + * Optimal Time Complexity: O(n) linear time is the best possible * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product */ export function calculateSumAndProduct(numbers) { - let sum = 0; - for (const num of numbers) { - sum += num; - } + // We went through the list two times, first to add the numbers, then to multiply them. + // But we can do both in one loop. This makes the code a bit simpler and faster. + + // let sum = 0; + // for (const num of numbers) { + // sum += num; + // } - let product = 1; - for (const num of numbers) { - product *= num; - } + // let product = 1; + // for (const num of numbers) { + // product *= num; + // } - return { - sum: sum, - product: product, - }; + // return { + // sum: sum, + // product: product, + // }; + + // My solution + + let sum = 0; + let product = 1; + for (const num of numbers) { + sum += num; + product *= num; + } + + return { + sum: sum, + product: product, + }; } + + diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..ecee3ab 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,33 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(m + n) — build set and loop through arrays once + * Space Complexity: O(n) — store second array in a set + * Optimal Time Complexity: O(m + 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))), -]; +// export const findCommonItems = (firstArray, secondArray) => [ +// ...new Set(firstArray.filter((item) => secondArray.includes(item))), +// ]; + +// My solution. Refactored to use a Set for faster lookups, making the code more efficient + +export const findCommonItems = (firstArray, secondArray) => { + // Turn secondArray into a Set to quickly check if items exist + const secondSet = new Set(secondArray); + // Create a Set to keep track of common items without duplicates + const commonItemsSet = new Set(); + + // Go through each item in firstArray + for (const element of firstArray) { + // If the item is found in secondSet, add it to commonItemsSet + if (secondSet.has(element)) { + commonItemsSet.add(element); + } + } + // Change the Set of common items back into an array to return + return [...commonItemsSet]; +}; diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..623b314 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,37 @@ /** * 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²) original (nested loops), O(n) refactored (using Set) + * Space Complexity: O(1) original, O(n) refactored (Set) + * Optimal Time Complexity: O(n) — in the refactored version using a Set for lookups * * @param {Array} 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; +// } +// } +// } +// return false; +// } + +// My solution - Refactored to use a Set for faster lookup, reducing time from O(n²) to O(n) 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; - } + const previousNumbers = new Set(); + + for (const num of numbers) { + const neededValue = target - num; + if (previousNumbers.has(neededValue)) { + return true; + } + previousNumbers.add(num); } - } - return false; + + return false; } + diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..14eacf6 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,51 @@ /** * 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²) original, O(n) optimised using Set + * Space Complexity: O(n) for 2 options + * Optimal Time Complexity: O(n) linear time is the best possible * * @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; +// } + export function removeDuplicates(inputSequence) { - const uniqueItems = []; + const seenItems = new Set(); + const uniqueSequence = []; - 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; - } + for (const item of inputSequence) { + if (!seenItems.has(item)) { + seenItems.add(item); + uniqueSequence.push(item); + } } - if (!isDuplicate) { - uniqueItems.push(inputSequence[currentIndex]); - } - } - return uniqueItems; + return uniqueSequence; } + diff --git a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py index cfd5cfd..36d20a4 100644 --- a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py +++ b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py @@ -12,20 +12,32 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: "sum": 10, // 2 + 3 + 5 "product": 30 // 2 * 3 * 5 } - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: O(2n) original (two passes), O(n) optimised (one pass) + Space Complexity: O(1) for both versions + Optimal time complexity: O(n) linear time is the best possible """ + # Edge case: empty list + # if not input_numbers: + # return {"sum": 0, "product": 1} + + # sum = 0 + # for current_number in input_numbers: + # sum += current_number + + # product = 1 + # for current_number in input_numbers: + # product *= current_number + + # return {"sum": sum, "product": product} + # Edge case: empty list if not input_numbers: return {"sum": 0, "product": 1} - sum = 0 - for current_number in input_numbers: - sum += current_number - - product = 1 + total_sum = 0 + total_product = 1 for current_number in input_numbers: - product *= current_number + total_sum += current_number + total_product *= current_number - return {"sum": sum, "product": product} + return {"sum": total_sum, "product": total_product}