We should read the question again and figure out what the question suggests us to do


  • 0
    W

    we should note "The order of elements can be changed".It just tell us do not move every element behind the elem you find.So we should use the swap() function.

    int removeElement(int A[], int n, int elem) {
        int frist = 0;
        int end = n-1;
        while(frist<=end)
        {
            if(A[frist]==elem)
            {
                swap(A[frist],A[end]);
                end--;
            }
            else
               frist++;
        }
        return end+1;
    }

Log in to reply
 

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