Delete duplicate element in sorted array

Given an array (1,1,2,2,3,3,4,4,5,5,6), return pointer to an array with no duplicates (1,2,3,4,5,6).  Hint: two pointers, in place, no extra memory.

C++:
01 int delduplicate(int* A, int size)
02 {
03     int t=1;
04     int* p=A;
05     int* q=p;
06     for(int i=1;i<size;i++){
07         q++;
08         if(*p!=*q){
09             t++;
10             p++;
11             *p=*q;
12         }
13     }
14 
15     return t;
16 }
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