My C++ Solution in O(n) time Avoid Considering the Special Situation when "A[n-1] == elem"


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

Log in to reply
 

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