Reputation:
this is for data structure, my professor wants me to write it with efficiency so if he finds one more efficient I'm done lol So... Is there a way to get it without using two loops? (don't use hashtags)
1 loop would be the most efficient
Thanks guys
Upvotes: 0
Views: 633
Reputation: 8515
Since it turned out to be useful, I'll post it as an answer. Maybe someone will use it too.
Here is the answer: http://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/
And example from the link:
// Java program to find intersection of
// two sorted arrays
class FindIntersection
{
/* Function prints Intersection of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
static void printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else
{
System.out.print(arr2[j++]+" ");
i++;
}
}
}
public static void main(String args[])
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = arr1.length;
int n = arr2.length;
printIntersection(arr1, arr2, m, n);
}
}
Upvotes: 2