简体   繁体   中英

Find intersection of two arrays

I have already read existing solutions but none satisfy my problem.

Problem: Given two arrays of known equal size n , create third array with common elements of both arrays. (Assume no duplicates in each array)

Example:

A[] = {5, 4, 12, 15, 9}
B[] = {4, 9, 7, 6, 12}

My solution:

I thought of first sorting the arrays into:

A[] = {4, 5, 9, 12, 15}
B[] = {4, 6, 7, 9, 12}

but then, I couldn't find out how I will find the common elements without avoiding nested loops. I have used nested loops like this:

int A[] = {5, 4, 12, 15, 9};
int B[] = {4, 9, 7, 6, 12};

// maximum common elements can be size of one array
int[] C = new int[A.length];
int idx = 0; // of C

// output check: all zero
for(int i = 0; i < C.length; i++)
    System.out.println(C[i]);

// find and transfer common elements
for(int i = 0; i < A.length; i++){
    for(int j = 0; j < B.length; j++){
        if(A[i] == B[j])
            C[idx++] = A[i];
    }
}

// output check
for(int i = 0; i < C.length; i++)
    System.out.println(C[i]);

But the problem with this approach is that, as expected, C[] = {4, 9, 12, 0, 0} ie last two elements are left zero because there were only three common elements. However, in case, the input arrays themselves had 0 s then it becomes difficult to say whether these zeros are present in C because they were common to both A and B or simply because there wasn't anymore common element to fill in (as happened above).

I can of course, first use a nested loop to count number of common elements m , then create an array C of size m , and then fill it with common elements of A and B using another nested loop, but that would be time consuming.

Limitations: I can't use HashSet, HashTable, or whatever ArrayUtils or anything what Java has to offer except simple sorting/searching algorithms (as such the former haven't been taught to me yet)

Question: What is the fastest approach for my query above? (taking in view the limitations I posted)

You can keep the separate counter for each array and increment on the basis of information you know. (Array is sorted).

  1. if the element is same then put the element in third array.
  2. if first is smaller then increment the counter for that array other wise increment for other array.
  3. the count will give you the number for common element in array.

public class SortedArray {
  public static void main(String[] args) {
    int a[] = {4, 5, 9, 12, 15};
    int b[] = {4, 6, 7, 9, 12};
    int c[] = new int[5];
    int count = 0;

    int i = 0; int j = 0;
    while (i<a.length && j<b.length) {
      if (a[i]==b[j]) {
        c[count] = a[i];
        count++;
        i++;
        j++;
      }
      else if (a[i] < b[j]) {
        i++;
      } else {
        j++;
      }
    }
    for (int k=0; k<count; k++) {
      System.out.print(c[k] + "\t");
    }
  }
}

Output :
4 9 12

You're already maintaining a count, idx , of the common elements. So there's no need to rely on a sentinel value like "0" to see which elements were actually found in common -- you already know how many were found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM