-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathremoveDuplicates.mjs
More file actions
19 lines (18 loc) · 918 Bytes
/
removeDuplicates.mjs
File metadata and controls
19 lines (18 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
*
* Time Complexity:before refactoring O(n²)- after refactoring O(n)
* Space Complexity:O(n)
* Optimal Time Complexity:O(n)
* The original implementation relied on nested loops to detect duplicates,
* resulting in O(n²) time complexity, which does not scale well for large inputs.
* The refactored solution uses a Set to perform duplicate checks in constant time.
* This eliminates the inner loop and reduces the overall time complexity to O(n),
* which is optimal for this problem.
*using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
return [...new Set(inputSequence)];
}