C# Code


  • 0
    M
    public class Solution {
        public int RemoveDuplicates(int[] nums) {
            if (nums == null) return 0;
            int writePointer = -1;
            for (int i=1;i<nums.Length; i++){
                if (nums[i] != nums[i-1] && writePointer != -1 ){
                    nums[writePointer] = nums[i];
                    writePointer++;
                }else {
                    if (writePointer ==-1 && nums[i] == nums[i-1]) writePointer = i; //found first repeted number
                }
                
            }
            return writePointer == -1 ? nums.Length : writePointer;
        }
    }
    

Log in to reply
 

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