Delete elements in an array

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.
size_t remove_elem(T* array, size_t len, T elem) {
}

C++:
01 template <typename T>
02 void swap(T* A, int i, int j)
03 {
04     T tmp=A[i];
05     A[i]=A[j];
06     A[j]=tmp;
07 }
08 
09 template <typename T>
10 size_t remove_elem(T* A, size_t len, T elem)
11 {
12     int i=0;
13     int j=len1;
14     while(true){
15         while(A[i]!=elem)
16             i++;
17         while(A[j]==elem)
18             j;
19         if(i>j)
20             return j+1;
21         else
22              swap(A,i,j);
23         i++;
24         j;
25     }
26 }
Advertisements
This entry was posted in Array and linked list. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s