LeetCode – Remove Duplicates from Sorted Array II (Java)

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?

For example, given sorted array A = [1,1,1,2,2,3], your function should return length = 5, and A is now [1,1,2,2,3].

So this problem also requires in-place array manipulation.

Java Solution 1

We can not change the given array's size, so we only change the first k elements of the array which has duplicates removed.

public class Solution {
	public int removeDuplicates(int[] A) {
		if (A == null || A.length == 0)
			return 0;
 
		int pre = A[0];
		boolean flag = false;
		int count = 0;
 
		// index for updating
		int o = 1;
 
		for (int i = 1; i < A.length; i++) {
			int curr = A[i];
 
			if (curr == pre) {
				if (!flag) {
					flag = true;
					A[o++] = curr;
 
					continue;
				} else {
					count++;
				}
			} else {
				pre = curr;
				A[o++] = curr;
				flag = false;
			}
		}
 
		return A.length - count;
	}
}

Java Solution 2

public class Solution {
	public int removeDuplicates(int[] A) {
		if (A.length <= 2)
			return A.length;
 
		int prev = 1; // point to previous
		int curr = 2; // point to current
 
		while (curr < A.length) {
			if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {
				curr++;
			} else {
				prev++;
				A[prev] = A[curr];
				curr++;
			}
		}
 
		return prev + 1;
	}
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Larry Okeke

    public static int remove_duplicates(int[] nums){

    int duplicates =0;

    for(int i = 0; i < nums.length; i++){

    if(i < nums.length-1 && nums[i] == nums[i+1]){

    int step = i++;

    while((i < nums.length-1 )&&nums[step] == nums[i++]){

    duplicates++;

    }

    }

    }

    return nums.length - duplicates;

    }

  • Swathi

    How about this? A slight modification on the remove duplicates I

    int[] removeDuplicatesN(int[] arr, int N){

    int i=0,j=1, l=0;

    int n= arr.length;

    while(j<n){

    if(arr[i]==arr[j]){

    j++;

    l++;

    if(l<N) i++;

    }

    else{

    l=0;

    i++;

    arr[i] = arr[j];

    j++;

    }

    }

    return Arrays.copyOf(arr, i+1);

    }

  • cp

    the better solution has a little bug. If an array [1,1] then it does not remove the element.

  • vincent

    Good!

  • Liang Tao

    I think what you said is the answer for “Remove Duplicates” problem, not this II version.

  • TK

    class Solution {
    public:
    int removeDuplicates(int A[], int n) {
    int currentPosition = 2;
    for (int i = 2; i < n; ++ i)
    if (A[i] != A[i-2])
    A[currentPosition++] = A[i];
    return min(currentPosition, n);
    }
    };

  • claire

    great solution

  • The better solution is actually confusing. We can make the rule like this:
    Assign previous pointer to 1st element(a[0]) and current pointer to 2nd element(a[1]).
    1) if a[prev] != a[curr], {prev++; a[prev]=a[cur]; cur++;}
    2) else, {cur++;}

    you can remove the A[curr] == A[prev – 1] in this case.

  • W Han

    In the first solution, the loop starts from the second element. If input array has only one element it will cause index out of bounds error.