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 @@ -9,21 +9,31 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Original code used two separate loops:
* - The first loop calculated the sum.
* - The second loop calculated the product.
*
* I changed the code to use one loop for both calculations.
* This means we do not need to go through the array twice.
*
* Time Complexity: O(n)
* Space Complexity: O(1)
* Optimal Time Complexity: O(n)
*
* Time complexity is O(n) because we need to visit every number.
* Space complexity is O(1) because we use a fixed number of variables.
* O(n) is the best possible time complexity for this task.
*
* @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;

// Calculate the sum and product in the same loop
for (const num of numbers) {
sum += num;
product *= num;
}

Expand Down
37 changes: 31 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* The original code used includes() to search the second array
* for each item in the first array. This can take a long time
* when the arrays are large.
*
* I changed the code to use a Set for faster searching.
* First, we store the items from the second array in a Set.
* Then, we check which items from the first array are in the Set.
* We use another Set to remove duplicate items from the result.
*
* Time Complexity: O(n + m)
* Space Complexity: O(n + m)
* Optimal Time Complexity: O(n + m)
*
* The original time complexity was O(n * m) because we searched
* the second array for every item in the first array.
*
* The new time complexity is O(n + m) on average because
* we create a Set and then check the items in the first array.
*
* The space complexity is O(n + m) because the Sets and the
* resulting array can store items from the input arrays.
*
* O(n + m) is optimal because we need to check the arrays
* to find their common items.
*
* @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 commonItems = firstArray.filter((item) => secondSet.has(item));

return [...new Set(commonItems)];
};
45 changes: 37 additions & 8 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* The original code used two loops to check every possible pair
* of numbers. This can take a long time when the array is large.
*
* I changed the code to use a Set to remember the numbers
* we have already checked.
*
* For each number, we calculate the number we need to find
* to reach the target. If we have already seen that number,
* we return true.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n)
*
* The original time complexity was O(n^2) because we used
* two loops to check the pairs of numbers.
*
* The new time complexity is O(n) on average because we
* only need to go through the array once.
*
* The space complexity is O(n) because the Set can store
* up to all the numbers in the array.
*
* O(n) is the optimal time complexity for this approach
* because we may need to check every number in the array.
*
* @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 seenNumbers = new Set();

for (const num of numbers) {
const neededNumber = target - num;

// Check if we have already seen the number we need
if (seenNumbers.has(neededNumber)) {
return true;
}

// Remember this number for the next checks
seenNumbers.add(num);
}

return false;
}
55 changes: 30 additions & 25 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
/**
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
* Remove duplicate values from a sequence, preserving the order
* of the first occurrence of each value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* The original code used two loops to check whether each value
* was already in the result array.
*
* I changed the code to use a Set. A Set stores each value only
* once and keeps the order in which values were added.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n)
*
* The original time complexity was O(n^2) because we searched
* the result array for every item in the input array.
*
* The new time complexity is O(n) on average because we go
* through the input array once.
*
* The space complexity is O(n) because we may need to store
* all the unique items in the Set and the returned array.
*
* O(n) is optimal because we need to check every item in
* the input array to find all unique values.
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];
// Create a Set to store unique items
const uniqueItems = new Set();

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]);
}
// Go through the input array and add each item to the Set
for (const item of inputSequence) {
uniqueItems.add(item);
}

return uniqueItems;
// Convert the Set back into an array
return [...uniqueItems];
}
Loading