|
1 | 1 | /** |
2 | 2 | * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. |
3 | 3 | * |
4 | | - * Time Complexity: |
5 | | - * Space Complexity: |
6 | | - * Optimal Time Complexity: |
| 4 | + * Time Complexity: O(n) - Single pass through the array |
| 5 | + * Space Complexity: O(n) - Set to track seen elements |
| 6 | + * Optimal Time Complexity: O(n) - Cannot do better than linear time |
7 | 7 | * |
8 | 8 | * @param {Array} inputSequence - Sequence to remove duplicates from |
9 | 9 | * @returns {Array} New sequence with duplicates removed |
10 | 10 | */ |
11 | 11 | export function removeDuplicates(inputSequence) { |
12 | | - const uniqueItems = []; |
| 12 | + // OPTIMIZED IMPLEMENTATION: O(n) time complexity |
| 13 | + // Previous implementation: O(n²) due to nested loops checking each element |
13 | 14 |
|
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]); |
| 15 | + const seen = new Set(); // O(n) |
| 16 | + const uniqueItems = []; // O(n) |
| 17 | + |
| 18 | + // O(n) time complexity |
| 19 | + for (const item of inputSequence) { |
| 20 | + // O(1) lookup |
| 21 | + if (!seen.has(item)) { |
| 22 | + seen.add(item); // O(1) operation |
| 23 | + uniqueItems.push(item); // O(1) operation |
32 | 24 | } |
33 | 25 | } |
34 | 26 |
|
35 | 27 | return uniqueItems; |
36 | 28 | } |
| 29 | +console.log(removeDuplicates([1, 2, 3, 4, 5, 1, 2, 3, 4, 5])); |
| 30 | +/* |
| 31 | + * ORIGINAL IMPLEMENTATION (for comparison): |
| 32 | + * |
| 33 | + * export function removeDuplicates(inputSequence) { |
| 34 | + * const uniqueItems = []; |
| 35 | + * |
| 36 | + * for (let currentIndex = 0; currentIndex < inputSequence.length; currentIndex++) { |
| 37 | + * let isDuplicate = false; |
| 38 | + * for (let compareIndex = 0; compareIndex < uniqueItems.length; compareIndex++) { |
| 39 | + * if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { |
| 40 | + * isDuplicate = true; |
| 41 | + * break; |
| 42 | + * } |
| 43 | + * } |
| 44 | + * if (!isDuplicate) { |
| 45 | + * uniqueItems.push(inputSequence[currentIndex]); |
| 46 | + * } |
| 47 | + * } |
| 48 | + * |
| 49 | + * return uniqueItems; |
| 50 | + * } |
| 51 | + * |
| 52 | + * COMPLEXITY ANALYSIS OF ORIGINAL: |
| 53 | + * - Outer loop: O(n) iterations through input array |
| 54 | + * - Inner loop: O(k) iterations through uniqueItems array (k grows with each unique element) |
| 55 | + * - Worst case: O(n²) when all elements are unique |
| 56 | + * - Space: O(n) for uniqueItems array |
| 57 | + * |
| 58 | + * PERFORMANCE ISSUES: |
| 59 | + * - Quadratic time complexity O(n²) in worst case |
| 60 | + * |
| 61 | + * IMPROVEMENTS MADE: |
| 62 | + * 1. Reduced from O(n²) to O(n) time complexity |
| 63 | + * 2. Set lookup is O(1) vs linear search O(k) |
| 64 | + * 3. Single pass through input array |
| 65 | + */ |
0 commit comments