-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDuplicates.js
More file actions
executable file
·36 lines (24 loc) · 1.03 KB
/
removeDuplicates.js
File metadata and controls
executable file
·36 lines (24 loc) · 1.03 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
// Remove duplicates from sorted array
// a classic two-pointer technique that teaches in-place array modification without extra space. Master the slow-fast pointer pattern essential for optimizing array operations and acing space complexity questions
// Modify the sorted array in-place to remove duplicates and return new length of the array
// example ----- input: nums = [1,1,3] | output = 2
// 1. Use slow pointer to track position for next unique element
// 2. Use fast pointer to scan through all elements
// 3. When new unique value found, increment slow and copy value
// O(n) space= O(1)
const arr = [1,1,2,2,3,3,4,4,4,4]
function removeDuplicates(nums) {
if(nums.length === 0) return 0;
// slow pointer for unique elements
let slow = 0;
// fast pointer to scan array
for(let fast = 1; fast < nums.length; fast++){
if(nums[fast] !== nums[slow]){
slow++;
nums[slow] = nums[fast];
}
}
console.log(nums)
return slow + 1;
}
console.log(removeDuplicates(arr));