Remove duplicate in sorted array

Given an array of sorted integers with duplicate values , sort the array so that there are only unique values left in sorted order ( do not use any additional data type , do inplace sort )

C++:
01 void swap(int& a, int& b)
02 {
03     int tmp=a;
04     a=b;
05     b=tmp;
06 }
07 
08 void sortQuick(int A[], int size, int start, int end)
09 {
10     if(start>=end)
11         return;
12     int piv=A[start];
13     int left=start+1;
14     int right=end;
15     while(true){
16         while(A[left]<piv)
17             left++;
18         while(A[right]>piv)
19             right;
20         if(left>=right)
21             break;
22         swap(A[left],A[right]);
23         left++;
24         right;
25     }
26     swap(A[start],A[right]);
27     sortQuick(A,size,start,right1);
28     sortQuick(A,size,right+1,end);
29 }
30 
31 void removeDuplicate(int* A, int size, int& sz)
32 {
33     int slow=0;
34     int fast=0;
35     int prv=A[0]1;
36     for(;fast<size;){
37         if(A[fast]!=prv)
38             prv=A[slow++]=A[fast];
39         fast++;
40     }
41     sz=slow;
42 }
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