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 @@ -16,19 +16,36 @@
* @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;
}
// 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,
// };
// }

let product = 1;
for (const num of numbers) {
product *= num;
}
// Refactored:

return {
sum: sum,
product: product,
};
export function calculateSumAndProduct(numbers) {
const result = numbers.reduce(
(acc, num) => {
((acc.sum += num), (acc.product *= num));
return acc;
},
{ sum: 0, product: 1 },
);
return result;
}

// * Time Complexity: O(n)
// * Space Complexity: O(1)
// * Optimal Time Complexity: O(n)
18 changes: 15 additions & 3 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
* @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) => {
let firstArr = new Set(firstArray);
let secondArr = new Set(secondArray);

return [...firstArr].filter((arr) => secondArr.has(arr));
};

// * https://www.w3schools.com/js/js_set_methods.asp#mark_set_new
// * Time Complexity: O(m+n)
// * Space Complexity: O(m+n)
// * Optimal Time Complexity: O(m+n)
29 changes: 24 additions & 5 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@
* @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;
// }

// To solve this problem I used this source: https://medium.com/@bloodturtle/finding-pairs-in-an-array-that-sum-to-a-target-value-b553e8c357bb

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 seen = new Set();
for (let num of numbers) {
let complement = target - num;
if (seen.has(complement)) {
return true;
}
seen.add(num);
}
return false;
}

// * Time Complexity: O(n)
// * Space Complexity: O(n)
// * Optimal Time Complexity: O(n)
// This solution is optimal because it uses a hash set(based on hash table logic) for constant-time lookups and processes each element once.
55 changes: 32 additions & 23 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,38 @@
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];
// 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]);
// }
// }

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]);
}
}
// return uniqueItems;
// }

return uniqueItems;
export function removeDuplicates(inputSequence) {
let noDuplicates = [...new Set(inputSequence)];
return noDuplicates;
}

// * Time Complexity: O(n)
// * Space Complexity: O(n)
// * Optimal Time Complexity: O(n)
Loading