Easy_understand java solution


  • 0
    J

    Use "count" to record the number of duplicates:

    class Solution {
        public int removeDuplicates(int[] nums) {
           int count=0;
            for(int i=0;i<nums.length-1;i++)
            {
                if(nums[i]==nums[i+1])
                    count++;//duplicate num 
                else
                    nums[i+1-count]=nums[i+1];
    
            }
            return nums.length-count;
        }
    }
    

Log in to reply
 

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