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 @@ -9,21 +9,22 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
*
* Time Complexity: O(n)
* Space Complexity: O(1)
* Optimal Time Complexity: O(n)
*
* We must visit every number at least once,
* so O(n) is already optimal.

* @param {Array<number>} 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) {
sum += num;
product *= num;
}

Expand All @@ -32,3 +33,10 @@ export function calculateSumAndProduct(numbers) {
product: product,
};
}
/*
Time Complexity is O(n) because every number in the array must be
processed once. Space Complexity is O(1) because only two variables
(sum and product) are stored regardless of the array size. O(n) is
also the optimal complexity because every element must be examined
at least once to calculate the correct sum and product.
*/
32 changes: 23 additions & 9 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n²)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n + m)
*
* Original complexity:
* The original solution used filter() and includes().
* filter() loops through every item in firstArray.
* For each item, includes() may need to search the entire
* secondArray. This results in O(n × m) time, which is
* O(n²) when both arrays are similar in size.
*
* @param {Array} firstArray - First array to compare
* @param {Array} secondArray - Second array to compare
* @returns {Array} Array containing unique common items
* Refactor:
* Convert secondArray into a Set. Set.has() provides
* constant-time lookups, so we only loop through each
* array once. This reduces the time complexity to O(n + m).
*
* Space Complexity:
* We store secondArray in a Set, which requires extra
* memory proportional to the size of secondArray.
*/
export const findCommonItems = (firstArray, secondArray) => [
...new Set(firstArray.filter((item) => secondArray.includes(item))),
];
export const findCommonItems = (firstArray, secondArray) => {
const secondSet = new Set(secondArray);

return [...new Set(firstArray.filter((item) => secondSet.has(item)))];

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 also take advantage of built-in Set operations such as union and intersect for better performance and simpler code.

};
43 changes: 31 additions & 12 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
/**
* 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²)
* Space Complexity: O(1)
* Optimal Time Complexity: O(n)
*
* @param {Array<number>} numbers - Array of numbers to search through
* @param {number} target - Target sum to find
* @returns {boolean} True if pair exists, false otherwise
* Original complexity:
* The original solution uses nested loops. For each number,
* it checks every remaining number in the array looking for
* a pair whose sum equals the target. Since each number can
* be compared with many other numbers, the time complexity
* is O(n²).
*
* Refactor:
* Use a Set to store numbers that have already been seen.
* For each number, calculate the value needed to reach the
* target and check whether it exists in the Set.
* Set lookups are O(1), so we only need one pass through
* the array.
*
* Refactored Complexity:
* Time Complexity: O(n)
* Space Complexity: 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 seen = new Set();

for (const num of numbers) {
const needed = target - num;

if (seen.has(needed)) {
return true;
}

seen.add(num);
}

return false;
}
}
47 changes: 23 additions & 24 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
/**
* 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)
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
* Original complexity:
* The original solution uses nested loops. For each item in
* the input sequence, it searches through the uniqueItems
* array to check whether the value already exists. This
* results in O(n²) time complexity in the worst case.
*
* Refactor:
* Use a Set to keep track of values that have already been
* seen. Set.has() and Set.add() are O(1) operations, allowing
* us to process the sequence in a single loop.
*
* Refactored Complexity:
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
export function removeDuplicates(inputSequence) {
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 item of inputSequence) {
if (!seen.has(item)) {
seen.add(item);
uniqueItems.push(item);
}
}

return uniqueItems;
}
}
Loading