[one pass] concise Java solution


  • 0
    J
    //One Pass!!
    public class Solution {
        public int removeDuplicates(int[] nums) {
            int start = 1, count = 1; 
            if(nums.length < 3) return nums.length;
            for(int i = 1; i < nums.length; i++){
                if(nums[i] == nums[i-1]){
                    count++;
                    if(count < 3) nums[start++] = nums[i];
                }else{
                    count = 1;
                    nums[start++] = nums[i];
                }
            }
            return start;
        }
    }

Log in to reply
 

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