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