My O(n) C# Solution


  • 0
    J

    public int RemoveDuplicates(int[] A) {

        int length = A.Length;
    
        int newLength = length;
    
        if (length > 1){
    
            int cur = A[0];
    
            int pointa = 0;//current allocated
    
            for (int i = 1; i < length; i++){
    
                if (A[i] == cur){
    
                    newLength --;
    
                }
    
                else{
    
                    pointa += 1;
    
                    A[pointa] = A[i];
    
                    cur = A[i];
    
                }
    
            }
    
        }
    
        return newLength;
    

    }


Log in to reply
 

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