Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ddde162
Refactor: Reduce looping to 1 process that performs both operation to…
HassanOHOsman Feb 25, 2026
01895f9
explain complexity using Big O
HassanOHOsman Feb 25, 2026
8fafe7b
Explain complexity using Big O Notation
HassanOHOsman Feb 25, 2026
45371a3
convert the second array to set so that we use its methid .has()
HassanOHOsman Feb 25, 2026
450ca3e
filter through the first array element to check if the set has the sa…
HassanOHOsman Feb 25, 2026
2ca3122
update the time complexity explanation to describe the state before t…
HassanOHOsman Feb 25, 2026
a29e73e
Explain the complexity using Big O notation
HassanOHOsman Feb 25, 2026
1344256
create set to achieve time efficieny - use it to store items we've al…
HassanOHOsman Feb 25, 2026
58344c5
Loop over each element in an array and check if the set contains the …
HassanOHOsman Feb 25, 2026
9b853cc
each loop add the check number into the set to ensure considering dis…
HassanOHOsman Feb 25, 2026
67f8aab
refactor and move set inside the function to avoid getting wrong results
HassanOHOsman Feb 25, 2026
c778939
update Optimal Time Complexity explanation
HassanOHOsman Feb 25, 2026
2916a36
Explain time and space complexities using Big O Notation
HassanOHOsman Feb 26, 2026
6b1203e
create a set instead of an array to store unique items. This is to av…
HassanOHOsman Feb 26, 2026
d9501bc
add one for loop that checks if each item in the array is present in …
HassanOHOsman Feb 26, 2026
7beaf4a
update logic so that the unique items are eventually stored in an arr…
HassanOHOsman Feb 26, 2026
ac594c9
explain optimal time complexity using Big O Notation
HassanOHOsman Feb 26, 2026
c757b24
updading code so tests are passed and result leads to unique common/s…
HassanOHOsman Feb 26, 2026
2d34b6d
update logic so that to avoid duplciation and only unique common numb…
HassanOHOsman Feb 26, 2026
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,37 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(2n) - Since we're looping through 2 separate loops
Copy link

Choose a reason for hiding this comment

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

In complexity analysis, we ignore the constant multiplier; O(2n) is treated the same as O(n).

Nevertheless, it is true that performance can usually be improved by reducing number of loops,

* Space Complexity: O(1) - only 2 variables used
* Optimal Time Complexity: O(n) - JS engine must read each number at least once
*
* @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) {
// product *= num;
// }

// return {
// sum: sum,
// product: 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 Down
30 changes: 24 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n * m) - filter checks each element of firstArray and includes scans secondArray
* Space Complexity: O(n) - filter creates a new array where size grows as the input of matches grows. Same thing with set.
* Optimal Time Complexity: O(n + m) - each element must be examined at least once; using a Set allows O(1) lookups
*
* @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) => [
// ...new Set(firstArray.filter((item) => secondArray.includes(item))),
// ];



export const findCommonItems = (firstArray, secondArray) => {
const arrayToSet = new Set(secondArray);
const uniqueCommonItems = [];
const checkedItems = new Set();

for (const element of firstArray) {
if (arrayToSet.has(element) && !checkedItems.has(element)) {
checkedItems.add(element);
uniqueCommonItems.push(element);
}

}
return uniqueCommonItems;
Comment on lines +20 to +31
Copy link

Choose a reason for hiding this comment

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

This work but code can be further simplified.

Optional challenge: Replace line 20-31 by one line of code.

Copy link
Author

Choose a reason for hiding this comment

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

Ok. I will look into it once I am done with the rest of backlog. Thank you for the feedback CJ :)

};
33 changes: 25 additions & 8 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
/**
* 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²) - since it's a nested loop over the same array.
* Space Complexity: O(1) - since there's no other array, set, object that gorws proportionally to the input size, space usage doesn't grow.
* Optimal Time Complexity: O(n) - each number is checked at least once after refactoring.
*
* @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;
// }
// }
// }
// return false;
// }



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;
}
let checkedNumbers = new Set();

for (const number of numbers) {
if (checkedNumbers.has(target - number)) {
return true;
}
checkedNumbers.add(number);
}
return false;

}

34 changes: 24 additions & 10 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
"""
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(n2) - nested loop run once per element n times.Same for outer loop
Space complexity: O(n) - since there's another array to store unique items. its size grow proportionally as the number of unique inputs grow.
Optimal time complexity: O(n) - For an array of n elements, you must look at each element at least a once to check if it's duplicate.
"""
# unique_items = []

# for value in values:
# is_duplicate = False
# for existing in unique_items:
# if value == existing:
# is_duplicate = True
# break
# if not is_duplicate:
# unique_items.append(value)

# return unique_items

unique_items = []
checked_values = set()

for value in values:
is_duplicate = False
for existing in unique_items:
if value == existing:
is_duplicate = True
break
if not is_duplicate:
if value not in checked_values:
unique_items.append(value)

checked_values.add(value)

return unique_items