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 @@ -5,30 +5,36 @@
* and the "product" is every number multiplied together
* so for example: [2, 3, 5] would return
* {
* "sum": 10, // 2 + 3 + 5
* "product": 30 // 2 * 3 * 5
* "sum": 10,
* "product": 30
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n)
* The function loops through the array once, processing each element exactly once.
*
* Space Complexity: O(1)
* Only two variables (sum and product) are used, regardless of the input size.
*
* Optimal Time Complexity: O(n)
* This is the optimal time complexity because every element must be read at least
* once to calculate both the sum and the product.
*
* @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 sum = 0
let product = 1

let product = 1;
// Refactored to use a single loop instead of two.
// This reduces the number of iterations but keeps the time complexity at O(n).
for (const num of numbers) {
product *= num;
sum += num
product *= num
}

return {
sum: sum,
product: product,
};
sum,
product,
}
}
15 changes: 11 additions & 4 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n × m)
* The function loops through the first array and uses `includes()` to search
* the second array for each element.
*
* Space Complexity: O(n)
* A new array is created to store the matching items.
*
* Optimal Time Complexity: O(n × m)
* The complexity cannot be reduced without using an additional lookup
* data structure such as a Set or a hash map.
*
* @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))),
];
]
26 changes: 17 additions & 9 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Original Time Complexity: O(n²)
* The original code uses nested loops to compare every possible pair of numbers.
*
* Space Complexity: O(n)
* A Set is used to store numbers that have already been seen.
*
* Optimal Time Complexity: O(n)
* The array is traversed once, and Set lookups are O(1) on average.
*
* @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
*/
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) {
if (seen.has(target - num)) {
return true
}

seen.add(num)
}
return false;

return false
}
43 changes: 13 additions & 30 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
/**
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
* Remove duplicate items from a sequence.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Original Time Complexity: O(n²)
* The original code uses a nested loop to compare every item with the
* previously collected unique items.
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
* Space Complexity: O(n)
* A new collection is created to store unique items.
*
* Optimal Time Complexity: O(n)
* A Set allows constant-time lookups, so each item only needs to be checked once.
*
* @param {Array} inputSequence - Sequence containing possible duplicates
* @returns {Array} Sequence containing only unique items
*/
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)]
}