File tree Expand file tree Collapse file tree 4 files changed +62
-48
lines changed
Expand file tree Collapse file tree 4 files changed +62
-48
lines changed Original file line number Diff line number Diff line change 99 * "product": 30 // 2 * 3 * 5
1010 * }
1111 *
12- * Time Complexity:
13- * Space Complexity:
14- * Optimal Time Complexity:
12+ * Areas of inefficiency in original version:
13+ * - Two separate passes over the array
14+ *
15+ * Time Complexity: O(n)
16+ * Space Complexity: O(1)
17+ * Optimal Time Complexity: O(n)
1518 *
1619 * @param {Array<number> } numbers - Numbers to process
1720 * @returns {Object } Object containing running total and product
1821 */
1922export function calculateSumAndProduct ( numbers ) {
2023 let sum = 0 ;
21- for ( const num of numbers ) {
22- sum += num ;
23- }
24-
2524 let product = 1 ;
25+
2626 for ( const num of numbers ) {
27+ sum += num ;
2728 product *= num ;
2829 }
2930
30- return {
31- sum : sum ,
32- product : product ,
33- } ;
34- }
31+ return { sum, product } ;
32+ }
Original file line number Diff line number Diff line change 11/**
22 * Finds common items between two arrays.
33 *
4- * Time Complexity:
5- * Space Complexity:
6- * Optimal Time Complexity:
4+ * Areas of inefficiency in original version:
5+ * - `secondArray.includes(item)` is O(m) and was called for each item in firstArray (n)
6+ * resulting in O(n * m) worst-case time.
7+ *
8+ * Time Complexity: O(n + m) average
9+ * Space Complexity: O(m + k) where:
10+ * - m = secondArray length (Set storage)
11+ * - k = number of unique common items (result Set storage)
12+ * Optimal Time Complexity: O(n + m)
713 *
814 * @param {Array } firstArray - First array to compare
915 * @param {Array } secondArray - Second array to compare
1016 * @returns {Array } Array containing unique common items
1117 */
12- export const findCommonItems = ( firstArray , secondArray ) => [
13- ...new Set ( firstArray . filter ( ( item ) => secondArray . includes ( item ) ) ) ,
14- ] ;
18+ export const findCommonItems = ( firstArray , secondArray ) => {
19+ const setB = new Set ( secondArray ) ;
20+ const common = new Set ( ) ;
21+
22+ for ( const item of firstArray ) {
23+ if ( setB . has ( item ) ) common . add ( item ) ;
24+ }
25+
26+ return [ ...common ] ;
27+ } ;
Original file line number Diff line number Diff line change 11/**
22 * Find if there is a pair of numbers that sum to a given target value.
3+ *
4+ * Areas of inefficiency in original version:
5+ * - Nested loops compare every possible pair.
6+ * - This results in quadratic time complexity as input size grows.
7+ *
8+ * Time Complexity: O(n²)
9+ * Space Complexity: O(1)
10+ * Optimal Time Complexity: O(n)
311 *
4- * Time Complexity:
5- * Space Complexity:
6- * Optimal Time Complexity:
7- *
12+ * - Use a Set to track numbers seen so far.
13+ * - For each number x, check whether (target - x) has already been seen.
14+ *
815 * @param {Array<number> } numbers - Array of numbers to search through
916 * @param {number } target - Target sum to find
1017 * @returns {boolean } True if pair exists, false otherwise
1118 */
1219export function hasPairWithSum ( numbers , target ) {
13- for ( let i = 0 ; i < numbers . length ; i ++ ) {
14- for ( let j = i + 1 ; j < numbers . length ; j ++ ) {
15- if ( numbers [ i ] + numbers [ j ] === target ) {
16- return true ;
17- }
20+ const seen = new Set ( ) ;
21+
22+ for ( const num of numbers ) {
23+ const complement = target - num ;
24+
25+ if ( seen . has ( complement ) ) {
26+ return true ;
1827 }
28+
29+ seen . add ( num ) ;
1930 }
31+
2032 return false ;
21- }
33+ }
Original file line number Diff line number Diff line change 11/**
22 * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
33 *
4- * Time Complexity:
5- * Space Complexity:
4+ * Areas of inefficiency in original version:
5+ * - Nested loop: for each element in inputSequence, it scanned uniqueItems so far.
6+ * That creates quadratic behavior in the worst case.
7+ *
8+ * Time Complexity: O(n²)
9+ * Space Complexity:O(n)
610 * Optimal Time Complexity:
711 *
812 * @param {Array } inputSequence - Sequence to remove duplicates from
913 * @returns {Array } New sequence with duplicates removed
1014 */
1115export function removeDuplicates ( inputSequence ) {
16+ const seen = new Set ( ) ;
1217 const uniqueItems = [ ] ;
1318
14- for (
15- let currentIndex = 0 ;
16- currentIndex < inputSequence . length ;
17- currentIndex ++
18- ) {
19- let isDuplicate = false ;
20- for (
21- let compareIndex = 0 ;
22- compareIndex < uniqueItems . length ;
23- compareIndex ++
24- ) {
25- if ( inputSequence [ currentIndex ] === uniqueItems [ compareIndex ] ) {
26- isDuplicate = true ;
27- break ;
28- }
29- }
30- if ( ! isDuplicate ) {
31- uniqueItems . push ( inputSequence [ currentIndex ] ) ;
19+ for ( const item of inputSequence ) {
20+ if ( ! seen . has ( item ) ) {
21+ seen . add ( item ) ;
22+ uniqueItems . push ( item ) ;
3223 }
3324 }
3425
You can’t perform that action at this time.
0 commit comments