26. Remove Duplicates from Sorted Array
Intitution
Complexity
Space Complexity
Time Complexity
Code
public int removeDuplicates(int[] nums) {
int i = 0, j = 0;
// Traverse the array
for(; i < nums.length;) {
// If first element or current is not equal to the previous (i.e., it's unique)
if(i == 0 || nums[i-1] != nums[i]) {
nums[j] = nums[i]; // Place the unique element at the next available position
j++; // Move the insert pointer
}
i++; // Move to the next element
}
return j; // j is the new length of the array with unique elements
}Last updated