Skip to content

Commit ea63137

Browse files
committed
Refactor Sprint 1 exercises
1 parent f13588d commit ea63137

4 files changed

Lines changed: 64 additions & 49 deletions

File tree

‎Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity:O(n) — two loops each visit every number.
13+
* Space Complexity: O(1) — uses a fixed number of variables.
14+
* Optimal Time Complexity: O(n) — every number must be checked.
1515
*
1616
* @param {Array<number>} numbers - Numbers to process
1717
* @returns {Object} Object containing running total and product
1818
*/
19+
1920
export function calculateSumAndProduct(numbers) {
2021
let sum = 0;
21-
for (const num of numbers) {
22-
sum += num;
23-
}
24-
2522
let product = 1;
2623
for (const num of numbers) {
24+
sum += num;
2725
product *= num;
2826
}
29-
3027
return {
3128
sum: sum,
3229
product: product,
3330
};
3431
}
32+
33+
/**
34+
After Refactor
35+
* Time Complexity: O(n) — one loop calculates both results.
36+
* Space Complexity: O(1) — uses a fixed number of variables.
37+
* Optimal Time Complexity: O(n) — every number must be checked.
38+
*/
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
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), where as well as m referes to a size — checks each item in the first array against items in the second array.
5+
* Space Complexity: O(n) — creates a filtered array and a Set.
6+
* Optimal Time Complexity: O(n + m) — each array can be checked in linear time using a Set.
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+
export const findCommonItems = (firstArray, secondArray) => {
13+
const secondSet = new Set(secondArray);
14+
const commonItems = new Set();
15+
16+
for (const item of firstArray) {
17+
if (secondSet.has(item)) {
18+
commonItems.add(item);
19+
}
20+
}
21+
22+
return [...commonItems];
23+
}
24+
25+
/**
26+
After Refactoring
27+
* Time Complexity: O(n + m) — uses a Set to check for common items efficiently.
28+
* Space Complexity: O(m + q) — stores items from the second array and the common items, where q is the number of unique matches.
29+
* Optimal Time Complexity: O(n + m) — each array is processed in linear time.
30+
*/
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
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(n²) — nested loops check pairs of numbers.
5+
* Space Complexity: O(1) — uses only a fixed number of variables.
6+
* Optimal Time Complexity:O(n) — a Set can find a matching pair in one pass, on average.
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
*/
1212
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-
}
13+
const seen = new Set();
14+
for (const number of numbers) {
15+
const needed = target - number;
16+
17+
if (seen.has(needed)) {
18+
return true;
1819
}
20+
21+
seen.add(number);
1922
}
23+
2024
return false;
2125
}
26+
27+
/*
28+
After Refactoring
29+
* Time Complexity: O(n) — checks each number once on average.
30+
* Space Complexity: O(n) — stores numbers in a Set.
31+
* Optimal Time Complexity: O(n) — one pass using a Set, on average.
32+
*/
Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
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 nested loops may compare many items against one another.
5+
* Space Complexity:O(n) — stores the unique items in a new array.
6+
* Optimal Time Complexity: O(n) — a Set can check for duplicates in one pass, on average.
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 uniqueItems = [];
13-
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]);
32-
}
33-
}
34-
35-
return uniqueItems;
12+
return [...new Set(inputSequence)];
3613
}
14+
15+
/*
16+
After Refactoring
17+
*Time Complexity: O(n) — uses a Set, on average.
18+
* Space Complexity: O(n) — stores unique items in a new collection.
19+
* Optimal Time Complexity: O(n) — each input item must be checked.
20+
*/

0 commit comments

Comments
 (0)