Java solution for any amount of duplicates


  • 0
    N
    public static int removeDuplicates(int[] nums) {
    		if (nums.length <= 1)
    			return nums.length;
    		int turtle = 0;
    		int max = nums[0];
    		int rabbit = 0;
    		while (rabbit < nums.length&&turtle<=nums.length-1) {
    			while (rabbit < nums.length && nums[rabbit] <= max)
    				rabbit++;
                turtle++;
    			if (rabbit < nums.length&&turtle<nums.length) {
    				max = nums[rabbit];
    				swap(nums, turtle, rabbit);
    			}
    		}
    		return turtle;
    	}
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.