Skip to content

Commit c9e2b94

Browse files
committed
analysis and refactoring
1 parent 134e683 commit c9e2b94

File tree

4 files changed

+44
-46
lines changed

4 files changed

+44
-46
lines changed

Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity:O(2n) originally have this as using 2 separate loops
13+
* Space Complexity:O(1) no extra space used that rows with input
14+
* Optimal Time Complexity:O(n) must at least visit each number once
1515
*
1616
* @param {Array<number>} numbers - Numbers to process
1717
* @returns {Object} Object containing running total and product
1818
*/
1919
export function calculateSumAndProduct(numbers) {
2020
let sum = 0;
21-
for (const num of numbers) {
22-
sum += num;
23-
}
24-
2521
let product = 1;
22+
//only one loop needed to calculate both sum and product
2623
for (const num of numbers) {
24+
sum += num;
2725
product *= num;
2826
}
2927

30-
return {
31-
sum: sum,
32-
product: product,
33-
};
28+
return {sum, product};
3429
}
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
/**
22
* Finds common items between two arrays.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity:O(n+m) it build set and loop through arrays once
5+
* Space Complexity: store second array in set
6+
* Optimal Time Complexity:O(m+n)
77
*
88
* @param {Array} firstArray - First array to compare
99
* @param {Array} secondArray - Second array to compare
1010
* @returns {Array} Array containing unique common items
1111
*/
12-
export const findCommonItems = (firstArray, secondArray) => [
13-
...new Set(firstArray.filter((item) => secondArray.includes(item))),
14-
];
12+
13+
// Refactored to use a Set for faster lookups, making the code more efficient
14+
export const findCommonItems = (firstArray, secondArray) => {
15+
const setB = new Set(secondArray);
16+
const common = new Set();
17+
18+
for (const item of firstArray) {
19+
if (setB.has(item)) {
20+
common.add(item);
21+
}
22+
}
23+
24+
return [...common];
25+
};
26+
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
/**
22
* Find if there is a pair of numbers that sum to a given target value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: o(n2) the function use 2 nested loop
5+
* Space Complexity:o(1)no additional significant memory used
6+
* Optimal Time Complexity: O(n) — in the refactored version using a Set for lookups
77
*
88
* @param {Array<number>} numbers - Array of numbers to search through
99
* @param {number} target - Target sum to find
1010
* @returns {boolean} True if pair exists, false otherwise
1111
*/
12+
//The original version used two nested loops, meaning it checked every possible pair of numbers in the array to see if they added up to the target. This approach has a time complexity of O(n²), which becomes very slow as the list grows larger.
1213
export function hasPairWithSum(numbers, target) {
13-
for (let i = 0; i < numbers.length; i++) {
14-
for (let j = i + 1; j < numbers.length; j++) {
15-
if (numbers[i] + numbers[j] === target) {
16-
return true;
17-
}
14+
const seen = new Set();
15+
16+
for (const num of numbers) {
17+
const complement = target - num;
18+
if (seen.has(complement)) {
19+
return true;
1820
}
21+
seen.add(num);
1922
}
23+
2024
return false;
2125
}

Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,21 @@
11
/**
22
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: O(n) — the function iterates through the input sequence once
5+
* Space Complexity: O(n) — in the worst case, all elements are unique and stored in the Set and output array
6+
* Optimal Time Complexity: O(n) — must at least visit each element once
77
*
88
* @param {Array} inputSequence - Sequence to remove duplicates from
99
* @returns {Array} New sequence with duplicates removed
1010
*/
1111
export function removeDuplicates(inputSequence) {
12+
const seen = new Set();
1213
const uniqueItems = [];
1314

14-
for (
15-
let currentIndex = 0;
16-
currentIndex < inputSequence.length;
17-
currentIndex++
18-
) {
19-
let isDuplicate = false;
20-
for (
21-
let compareIndex = 0;
22-
compareIndex < uniqueItems.length;
23-
compareIndex++
24-
) {
25-
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
26-
isDuplicate = true;
27-
break;
28-
}
29-
}
30-
if (!isDuplicate) {
31-
uniqueItems.push(inputSequence[currentIndex]);
15+
for (const item of inputSequence) {
16+
if (!seen.has(item)) {
17+
seen.add(item);
18+
uniqueItems.push(item);
3219
}
3320
}
3421

0 commit comments

Comments
 (0)