My solution for Remove Element


  • 0
    T
       class Solution {   
    public:
    int removeElement(int A[], int n, int elem) {
        int i=0;
        int j=n-1;
        while(i<=j){
            if(A[i]==elem&&A[j]!=elem){
                A[i]=A[j];
                i++;j--;
            }
            if(A[i]==elem&&A[j]==elem){
                j--;
            }
            if(A[i]!=elem){
                i++;
            }
        }
        return j+1;
    }
    };

Log in to reply
 

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