This is an interview question that I am using as a programming exercise.
Input: Two sorted integer arrays A and B in increasing order and of different sizes N and M, respectively
Output: A sorted integer array C in increasing order that contains elements that appear in both A and B
Contraints: No duplicates are allowed in C
Example: For input A = {3,6,8,9} and B = {4,5,6,9,10,11}, the output should be C = {6,9}
Thank you for your answers, all! To summarize, there are two main approaches to this problem:
My original solution was to keep two pointers, one for each array, and scanning the arrays from left to right interchangeably, while picking out elements that match. So when we the current element of one array is larger than the second array, we keep incrementing the pointer of the second array until we either find the current first array element or overpass it (find one larger). I keep all matched in a separate array, which is returned once we reach the end of either one of the input arrays.
Another way that we could do this is to scan one of the arrays linearly, while using binary search to find a match in the second array. This would mean O(N*log(M)) time, if we scan A and for each of its N elements binary search on B (O(log(M)) time).
I've implemented both approaches and ran an experiment to see how the two compare (details on this can be found here). The Binary Search method seems to win when M is roughly 70 times larger than N, when N has 1 million elements.