Find duplicate elements in two sorted array.

Two sorted arrays, find the duplicate items. 

C++:
01 void findDuplicate(int* A, int *B, int sza, int szb, vector <int>& duplicate)
02 {
03     int p=0;
04     int q=0;
05     while((p<sza)&&(q<szb)){
06         if(A[p]==B[q]){
07             duplicate.push_back(A[p]);
08             p++;
09             q++;
10             continue;
11         }
12         if(A[p]<B[q])
13             p++;
14         else
15             q++;
16     }
17 }
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