Easy to understand Java Solution:


  • 1

    Save count for distinct numbers.
    If the count is less than 3 we can keep it.


    public class Solution {
        public int removeDuplicates(int[] nums) {
            int cnt=0;
            int len=0;
            for(int i=0;i<nums.length;++i){
                if(i==0 || nums[i]!=nums[i-1]){
                    cnt = 1;
                }else{
                    cnt++;
                }
                if(cnt<3){
                    nums[len++] = nums[i];
                }
            }
            return len;
        }
    }

Log in to reply
 

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