Really like how clever this solution is. It's definitely O(n) time complexity, unlike my solution (JavaScript, below) which has worst case O(n^2) because of the re-indexing whenever splice() is called.

var removeDuplicates = function(nums) {
let previousNum = null;
for (var i = nums.length - 1; i >= 0; i--) {
var num = nums[i];
if (num === previousNum) {
nums.splice(i, 1);
}
previousNum = num;
}
return nums.length;
};

Hi Sen5, It seems for this problem, the given array is sorted. For your test case, this algorithm is not appropriate. Let me know if you have any additional questions.

public int removeDuplicates(int[] A) {
int count=0;
int len = A.length;
for(int i=1;i<len;i++){
if(A[i]==A[i-1]){
++count;
}
else if(count>0){
A[i-count]=A[i];
}
}
return len-count;
}