How to find the intersection of two arrays

Source: Internet
Author: User
Tags arrays comparison rand

The title is probably like this: given two large arrays (1w or more below 100 million), find the intersection of two arrays in the most efficient way.

For this problem, I have a idea is to sort the array first, and then use two pointers in the sorted arrays on the rotation point to the head node, for comparison.

The brighter part lies in the way of comparison.

First, when comparing, first determine whether the internal use of two pointers is consistent. If it's consistent, then this point is an element of intersection, okay?

And here's the question, how do we compare next?

The steps are as follows: Compare the two pointers to the size of the content, point to the pointer with a small result, and start incrementing until the smaller pointer points to a value greater than or equal to another pointer.

And then another pointer takes the same approach, at which point the larger pointer has become a smaller pointer, incrementing until it is greater than or equal to another pointer.

When the above two-wheel comparison is complete, if the value pointed to is equal, then save the data, and do the same data processing, the code will be reflected.

Then two pointers + +, then the next round of comparison is OK.

Using this method, we can find the intersection of two large arrays, and the efficiency is good. If the length of the two arrays is M and N, the time required to calculate the speed of the row, then the total time efficiency is:

O (Nlog (n) + Mlog (m) + M + N) should be said to be good.

Space efficiency is an O (1)//not intersection of data storage

First of all: If you think the code appears in the English annotation is not that I wrote the code, then I can only say: you out ~

In fact, the main reason is convenient, and codeblocks on the man is very difficult to see ...

In addition, my code used a program to generate two of large random data files, the number is 1w and 2w respectively. The random data file generates the following code:

#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <ctime>
    
using namespace std;
    
int main ()
{
    cout << "Hello world!" << Endl;
    Ofstream Fout;
    Vector<int> Arrayone;
    Vector<int> Arraytwo;
    int n = 10000;
    int m = 20000;
    Srand (Time (NULL));
    for (int i = 0;i < n;++ i)
        Arrayone.push_back (rand ());
    for (int i = 0;i < m;++ i)
        Arraytwo.push_back (rand ());
    Fout.open ("A.txt", Ios_base::out | ios_base::trunc);
    for (int i = 0;i < n;++ i)
        fout << arrayone[i] << ends;
    Fout.close ();
    
    Fout.open ("B.txt", Ios_base::out | ios_base::trunc);
    for (int i = 0;i < m;++ i)
        fout << arraytwo[i] << ends;
    Fout.close ();
    return 0;
}

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.