My JAVA solution


  • 0
    Q

    Can be easily modified to allow for different numbers of duplicates.

     public class Solution {
        public int removeDuplicates(int[] nums) {
            return removeDuplicates(nums, 2);
        }
        public int removeDuplicates(int[] nums, int numSameAllowed) {
            int count = nums.length;
            if (count>=numSameAllowed){
            	count = numSameAllowed;
            }
            for (int i=numSameAllowed,pointer=numSameAllowed;i<nums.length;i++,pointer++){
            	if (nums[i] <= nums[i-numSameAllowed]){
    	        	while (pointer<nums.length  && nums[i-numSameAllowed]==nums[pointer]){
    	        		pointer++;
    	        	}
            	}
            	if (pointer<nums.length){
            		nums[i] = nums[pointer];
            		if (nums[i]!=nums[i-numSameAllowed]){
            			count++;
            		}
            		else{
            		    break;
            		}
            	}
            	else{
            	    break;
            	}
            }
            return count;
        }
    }

Log in to reply
 

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