Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Edit and Remove Duplicates in an array
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// Start slow and fast from index k since first k elements are always allowed.
// Only copy current fast element to slow if it's different from the element k places behind.
// This ensures each number appears at most k times in the result.

// time o(n)
//space o(1)
class Solution {
public int removeDuplicates(int[] nums) {

int k = 2;
int slow = k, fast = k;

while(fast < nums.length){

if(nums[slow - k] != nums[fast]){
nums[slow] = nums[fast];
slow++;
}

fast++;
}

return slow;
}
}