Integer Array Processing Algorithm (5) finding the common elements of two ordered arrays

Source: Internet
Author: User

// Find the common elements of the two Arrays: int * FindCommon (int * a, int * B, int nA, int nB, int & nOut) {int I = 0; int j = 0; vector <int> vec_comm; int * output = NULL; while (I <nA & j <nB) {if (a [I] <B [j]) ++ I; else if (a [I] = B [j]) {vec_comm.push_back (a [I]); cout <a [I] <endl; ++ I; ++ j;} else // a [I]> B [j] ++ j ;} nOut = vec_comm.size (); int nCoun = 0; if (nOut> 0) {output = new int [nOut]; vector <int >:: iterator itorComm; f Or (itorComm = vec_comm.begin (); itorComm! = Vec_comm.end (); itorComm ++) {output [nCoun] = * itorComm; nCoun ++ ;}} return output ;}

Test code:

int main(){int* a= new int[6];int* b= new int[3];a[0]=2;a[1]=3;a[2]=4;a[3]=6;a[4]=8;a[5]=9;b[0]=8;b[1]=9;b[2]=10;int nOut = 0;int* output = FindCommon(a, b, 6, 3, nOut);for (int i=0; i<nOut; i++){cout << output[i] << "," ;}cout << endl;delete[] a;a=NULL;delete[] b;b=NULL;delete[] output;output=NULL;cout << endl;return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.