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

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.

class Solution {    
public int removeDuplicates(int[] nums) {
// write your code here
if(nums == null)
return 0;
int cur = 0;
int i ,j;
for(i = 0; i < nums.length;){
int now = nums[i];
for( j = i; j < nums.length; j++){
if(nums[j] != now)
break;
if(j-i < 2)
nums[cur++] = now;
}
i = j;
}
return cur;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int target = 0;
int curnum = -1;
int count = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == curnum)
count++;
else {
curnum = nums[i];
count = 1;
}

if(count < 3) {
nums[target] = curnum;
target++;
}
}
return target;
}
}