O(N) Time and O(1) Java Solution When Allowed at Most K times of Duplicates


  • 9
    S

    Share my general solution for "Remove Duplicates Problem".

    If anyone could think of a better solution please let me know.

    public int removeDuplicates(int[] nums) {
        		//define at most k times of duplicate numbers
        		final int k = 2;
    
        		//check if it is an empty array
        		if(nums.length == 0) return 0;
    
        		//start pointer of new array
        		int m = 1;
    
        		// count the time of duplicate numbers occurence
        		int count = 1;
    
        		for(int i = 1; i < nums.length; ++i) {
        			if(nums[i] == nums[i - 1]) {
        				if(count < k) {
        					nums[m++] = nums[i];
        				}
        				count++;
        			} else {
        				count = 1;
        				nums[m++] = nums[i];
        			}
        		}
        		return m;
        	}

Log in to reply
 

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