diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..80c334e 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,21 +9,37 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(2n) - Since we're looping through 2 separate loops + * Space Complexity: O(1) - only 2 variables used + * Optimal Time Complexity: O(n) - JS engine must read each number at least once * * @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; +// } + +// let product = 1; +// for (const num of numbers) { +// product *= num; +// } + +// return { +// sum: sum, +// product: product, +// }; +// } + 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; } diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..2134837 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,32 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n * m) - filter checks each element of firstArray and includes scans secondArray + * Space Complexity: O(n) - filter creates a new array where size grows as the input of matches grows. Same thing with set. + * Optimal Time Complexity: O(n + m) - each element must be examined at least once; using a Set allows O(1) lookups * * @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))), +// ]; + + + +export const findCommonItems = (firstArray, secondArray) => { + const arrayToSet = new Set(secondArray); + const uniqueCommonItems = []; + const checkedItems = new Set(); + + for (const element of firstArray) { + if (arrayToSet.has(element) && !checkedItems.has(element)) { + checkedItems.add(element); + uniqueCommonItems.push(element); + } + + } + return uniqueCommonItems; +}; \ No newline at end of file diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..8e0b8e8 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,38 @@ /** * 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²) - since it's a nested loop over the same array. + * Space Complexity: O(1) - since there's no other array, set, object that gorws proportionally to the input size, space usage doesn't grow. + * Optimal Time Complexity: O(n) - each number is checked at least once after refactoring. * * @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; +// } + + + 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; - } + let checkedNumbers = new Set(); + + for (const number of numbers) { + if (checkedNumbers.has(target - number)) { + return true; } + checkedNumbers.add(number); } return false; + } + diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..3771a52 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -7,19 +7,33 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: """ 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) - nested loop run once per element n times.Same for outer loop + Space complexity: O(n) - since there's another array to store unique items. its size grow proportionally as the number of unique inputs grow. + Optimal time complexity: O(n) - For an array of n elements, you must look at each element at least a once to check if it's duplicate. """ + # unique_items = [] + + # for value in values: + # is_duplicate = False + # for existing in unique_items: + # if value == existing: + # is_duplicate = True + # break + # if not is_duplicate: + # unique_items.append(value) + + # return unique_items + unique_items = [] + checked_values = set() for value in values: - is_duplicate = False - for existing in unique_items: - if value == existing: - is_duplicate = True - break - if not is_duplicate: + if value not in checked_values: unique_items.append(value) - + checked_values.add(value) + return unique_items + + + + \ No newline at end of file