Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Initial Time Complexity: O(n) [multiple passes]
* Space Complexity: O(1)
* Optimal Time Complexity: O(n) [single pass]
*
* @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;
}

return {
sum: sum,
product: product,
sum,
product,
};
}
23 changes: 17 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Initial Time Complexity: O(n × m) [filter with includes]
* Space Complexity: O(n + m)
* Optimal Time Complexity: O(n + m) [with Set]
*
* @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) => {
const secondSet = new Set(secondArray);
const seen = new Set();
const result = [];

for (const item of firstArray) {
if (secondSet.has(item) && !seen.has(item)) {
seen.add(item);
result.push(item);
}
}

return result;
};
18 changes: 10 additions & 8 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Initial Time Complexity: O(n²) [nested loops]
* Space Complexity: O(n)
* Optimal Time Complexity: O(n) [with Set]
*
* @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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of set


for (const num of numbers) {
if (seen.has(target - num)) {
return true;
}
seen.add(num);
}

return false;
}
29 changes: 8 additions & 21 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
/**
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Initial Time Complexity: O(n²) [nested loops]
* Space Complexity: O(n)
* Optimal Time Complexity: O(n) [with Set]
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
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);
}
}

Expand Down