Skip to content
Closed
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,19 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n) - two passes through the array
* Space Complexity: O(1) - only two variables
* Optimal Time Complexity: O(n) - single pass through the array
*
* @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) {
sum += num;
product *= num;
}

Expand Down
23 changes: 17 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n * m) - filter iterates firstArray, includes iterates secondArray
* Space Complexity: O(n) - Set and result array
* Optimal Time Complexity: O(n + m) - convert secondArray to Set for 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) => {
const secondSet = new Set(secondArray);
const seen = new Set();
const result = [];

for (const item of firstArray) {
if (secondSet.has(item) && !seen.has(item)) {
seen.add(item);
result.push(item);
}
}

return result;
};
19 changes: 11 additions & 8 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/**
* 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^2) - nested loops compare every pair
* Space Complexity: O(1) - no extra data structures
* Optimal Time Complexity: O(n) - using a Set to store seen numbers
*
* @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 seen = new Set();

for (const num of numbers) {
const complement = target - num;
if (seen.has(complement)) {
return true;
}
seen.add(num);
}

return false;
}
29 changes: 8 additions & 21 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
/**
* 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(n^2) - inner loop checks all unique items for each element
* Space Complexity: O(n) - stores unique items in array
* Optimal Time Complexity: O(n) - using a Set for O(1) lookups
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const seen = new Set();
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 (const item of inputSequence) {
if (!seen.has(item)) {
seen.add(item);
uniqueItems.push(item);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
"sum": 10, // 2 + 3 + 5
"product": 30 // 2 * 3 * 5
}
Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: O(n) - two passes through the list
Space Complexity: O(1) - only two variables
Optimal time complexity: O(n) - single pass through the list
"""
# Edge case: empty list
if not input_numbers:
return {"sum": 0, "product": 1}

sum = 0
for current_number in input_numbers:
sum += current_number

total = 0
product = 1

for current_number in input_numbers:
total += current_number
product *= current_number

return {"sum": sum, "product": product}
return {"sum": total, "product": product}
18 changes: 11 additions & 7 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ def find_common_items(
"""
Find common items between two arrays.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: O(n * m) - nested loops compare every pair
Space Complexity: O(n) - stores common items in list
Optimal time complexity: O(n + m) - convert second_sequence to set for O(1) lookups
"""
second_set = set(second_sequence)
seen = set()
common_items: List[ItemType] = []
for i in first_sequence:
for j in second_sequence:
if i == j and i not in common_items:
common_items.append(i)

for item in first_sequence:
if item in second_set and item not in seen:
seen.add(item)
common_items.append(item)

return common_items
18 changes: 11 additions & 7 deletions Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
"""
Find if there is a pair of numbers that sum to a target value.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: O(n^2) - nested loops compare every pair
Space Complexity: O(1) - no extra data structures
Optimal time complexity: O(n) - using a set to store seen numbers
"""
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[i] + numbers[j] == target_sum:
return True
seen = set()

for num in numbers:
complement = target_sum - num
if complement in seen:
return True
seen.add(num)

return False
15 changes: 6 additions & 9 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ 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(n^2) - inner loop checks all unique items for each element
Space complexity: O(n) - stores unique items in list
Optimal time complexity: O(n) - using a set for O(1) lookups
"""
seen = set()
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:
if value not in seen:
seen.add(value)
unique_items.append(value)

return unique_items
Loading