Accepted Java Solution By putting the removed element on the left of the array


  • 0
    L
    public int removeElement(int[] A, int elem) {   
            if(A==null||A.length==0)
                return 0;
            int i=0;
            int j=A.length-1;
            while(i<j)
            {
                if(A[i]==elem)
                {
                    A[i]=A[j];
                    j--;
                }
                else
                    i++;
            }
            if(A[i]==elem)
                i--;
            return i+1;
        }

Log in to reply
 

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