#include<iostream>

using namespace std;

int removeElement(int A[], int n, int elem) {
    if (n == 0) {
        return n;
    }

    if (n == 1 && A[0] == elem) {
        return 0;
    }

    if (n == 1 && A[0] !=  elem) {
        return n;
    }

    int tail = n - 1;
    int i = 0;
    while (i <= tail) {
        if (A[i] == elem) {
            A[i] = A[tail];
            --tail;
        } else {
            ++i;
        }
    }

    return tail + 1;
}

int main() {
    int A[] = {1,2,3};

    int length = removeElement(A, 3, 1);
    for(int i = 0; i < length; ++i) {
        cout << A[i] << endl;
    }

    cout << endl;

    int B[] = {1,1};

    length = removeElement(B, 2, 1);
    for(int i = 0; i < length; ++i) {
        cout << B[i] << endl;
    }
    return 0;
}

#include<iostream>

using namespace std;

int removeElement(int A[], int n, int elem) {
    int length = n-1;
    for(int i = 0; i <= length; ++i) {
        if(A[i] == elem) {
            while (A[length] != elem) {
                length--;
            }
            A[i] = A[length--];
        }
    }

    return length + 1;
}

int main() {
    int A[] = {1,2,3,4,1,6,3,4};

    int length = removeElement(A, 8, 1);
    for(int i = 0; i < length; ++i) {
        cout << A[i] << endl;
    }

    return 0;
}

--- 
+++ 
@@ -3,26 +3,47 @@
 using namespace std;
 
 int removeElement(int A[], int n, int elem) {
-    int length = n-1;
-    for(int i = 0; i <= length; ++i) {
-        if(A[i] == elem) {
-            while (A[length] != elem) {
-                length--;
-            }
-            A[i] = A[length--];
+    if (n == 0) {
+        return n;
+    }
+
+    if (n == 1 && A[0] == elem) {
+        return 0;
+    }
+
+    if (n == 1 && A[0] !=  elem) {
+        return n;
+    }
+
+    int tail = n - 1;
+    int i = 0;
+    while (i <= tail) {
+        if (A[i] == elem) {
+            A[i] = A[tail];
+            --tail;
+        } else {
+            ++i;
         }
     }
 
-    return length + 1;
+    return tail + 1;
 }
 
 int main() {
-    int A[] = {1,2,3,4,1,6,3,4};
+    int A[] = {1,2,3};
 
-    int length = removeElement(A, 8, 1);
+    int length = removeElement(A, 3, 1);
     for(int i = 0; i < length; ++i) {
         cout << A[i] << endl;
     }
 
+    cout << endl;
+
+    int B[] = {1,1};
+
+    length = removeElement(B, 2, 1);
+    for(int i = 0; i < length; ++i) {
+        cout << B[i] << endl;
+    }
     return 0;
 }
Remove an element from a list completed
