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,26 +9,26 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: is o(n) - coz only we have one input that needs at least one loop
* Space Complexity: here the space is o(1) coz the memory will not growing whatever happen with input
* Optimal Time Complexity: here even when i drop one of the loops the optimal time complexity will be still O(n) because we dealing with input need at least one loop
*
* @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;
sum += num;
product *= num
}

return {
sum: sum,
product: product,
};
}


console.log(calculateSumAndProduct([2, 3, 8, 9]));
15 changes: 9 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: now is O(n^2) - coz we doing instead loop one with the filter first array then we checking the array with includes loop to find out if the item are common in both arrays
* Space Complexity: is O(n) - coz it is grows directly proportional with the input size
* Optimal Time Complexity: to O(n), after i used a Set, now the lookup is faster instead of checking the whole second array each time
*
* @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) => {
let second = new Set(secondArray)
return [ ...new Set(firstArray.filter((item) => second.has(item))) ]
}
console.log(findCommonItems([1, 2, 2, 3], [4, 3, 2]))

Comment on lines +12 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note: A set data structure typically supports set operations such as union, intersect, etc. You could also explore using these methods to simplify your solution.

22 changes: 14 additions & 8 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: is O(n^2) coz we have nested loop in the function
* Space Complexity: is O(n) coz it grows proportionally with the input size
* Optimal Time Complexity: to O(n) - and that by using Set as check point to fast lookup and catch up if it has the number instead of re-scanning everything
*
* @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 checkPoint = new Set()

for(let i=0; i < numbers.length; i++){
let num = target - numbers[i]
if(checkPoint.has(num)){
return true
}else {
checkPoint.add(numbers[i])
Comment on lines +15 to +20

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The formatting of the code is not quite consistent.

More importantly, have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode?
See https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md for more info.

}
}

return false;
}

console.log(hasPairWithSum([1, 2, 4, 5, 3], 9));
32 changes: 7 additions & 25 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
/**
* 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: is O(n^2) coz it used nested loop to remove the duplications
* Space Complexity: it is O(n) coz it is grows directly proportional with the input size
* Optimal Time Complexity: to O(n) - because even if i'm using Set, i need to go through each element in the array, which leads me to the time complexity will be growing directly proportional with the input size
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
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]);
}
}
const uniqueItems = [...new Set(inputSequence)]

return uniqueItems;
}

console.log(removeDuplicates([5, 2, 2, 3, 4, 4, 1]))
console.log(removeDuplicates(["a", "e", "b", "a", "c", "c", "a"]))
Loading