-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathremoveDuplicates.mjs
More file actions
31 lines (29 loc) · 1.02 KB
/
removeDuplicates.mjs
File metadata and controls
31 lines (29 loc) · 1.02 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
/**
* Remove duplicate values from a sequence, preserving the order of the first occurrence.
*
* Time Complexity:
* Before refactoring: O(n^2)
* The original solution used nested loops to check for duplicates.
* After refactoring: O(n)
* Using a Set allows duplicate checks in constant time (O(1)),
* removing the need for the inner loop.
*
* Space Complexity:
* Before refactoring: O(n)
* The result array could store up to n unique items.
* After refactoring: O(n)
* The Set and the final array both store up to n unique items.
*
* Optimal Time Complexity:
* O(n) — You must inspect each element at least once, and Set lookups are O(1).
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniques =new Set() //does dupliacte check in O(1)
for (let i=0 ;i<inputSequence.length;i++){ // O(n)
uniques.add(inputSequence[i]);
}
return Array.from(uniques);
}