Shortest solution! Can be used for At Most K Duplicates as weill


#1
	public int removeDuplicates(ArrayList<Integer> a) {
	    //Here at most duplicates allowed is 2, can be scaled to N duplicates
	    int at_most = 2;
	    //Index pointer to new modified array.
	    int j = 0;
	    //Handle corner cases of size less than 2.
	    if(a.size() <=2)
	        return a.size();
	        
	    for(int i=0; i<a.size(); i++)
	    {   
	        //Since corner cases are handled above, blindly add first two elements into same
	        //modified array.
	        //After filling first two elements check if the current element > 2 elements before the added array element.
	        if(j < at_most || a.get(i) > a.get(j-at_most))
	        {
	            a.set(j++,a.get(i));
	        }
	    }
	    return j;
	}
}

#2

Brilliant solution. Thanks much