Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 n--; 7 for(int i=0; i<=n; i++){ 8 if(A[i] == elem){ 9 while(n>i && A[n] == elem){ 10 n--; 11 } 12 A[i] = A[n]; 13 n--; 14 } 15 } 16 return n+1; 17 } 18 };
Run Status: Accepted!
Program Runtime: 20 milli secs