Common Elements Of Three Arrays
March 17, 2015
If I had to write that program while standing in front of an interviewer writing on a whiteboard, I think I would take the simple approach: scan through the first two arrays, keeping common elements, then scan the result with the third array. Here’s the two-array version, using lists because that is simpler in Scheme:
(define (common2 xs ys)
(let loop ((xs xs) (ys ys) (zs (list)))
(cond ((or (null? xs) (null? ys))
(reverse zs))
((< (car xs) (car ys))
(loop (cdr xs) ys zs))
((< (car ys) (car xs))
(loop xs (cdr ys) zs))
(else (loop (cdr xs) (cdr ys)
(cons (car xs) zs))))))
I wrote that about as fast as I can type, and it worked the first time; it’s a standard recursion that every Scheme programmer internalizes quickly. Then it’s easy to extend to three lists:
(define (common3 xs ys zs)
(common2 (common2 xs ys) zs))
And here’s the example problem:
> (define xs '(1 5 10 20 40 80))
> (define ys '(6 7 10 20 80 100))
> (define zs '(3 4 15 20 30 70 80 120))
> (common2 xs ys)
(10 20 80)
> (common2 ys zs)
(20 80)
> (common2 xs zs)
(20 80)
> (common2 (common2 xs ys) zs)
(20 80)
> (common3 xs ys zs)
(20 80)
> (common3 '(1 5 5 5) '(3 4 5 5 10) '(5 5 10 20))
(5 5)
As an added bonus, this approach generalizes to more than three inputs in an obvious way:
(define (common . xss)
(fold-left common2 (car xss) (cdr xss)))
> (common xs ys zs)
(20 80)
This algorithm is reasonably efficient, running in time O(kn) where k is the number of lists and n is the length of the second-longest list. It’s also simple to code and hard to get wrong, and I could be confident writing it on a whiteboard while an interviewer watched. If you want to have some fun, look at the solutions at Career Cup and try to convince yourself that they are correct.
You can run the program at http://ideone.com/ER0ChF.
My solution is to find shortest two arrays to process biggest one only one time.
This allows for searching for only common integers from the two shortest arrays in bigest one.
Output:
20
80
999
999
Haskell:
There is probably a more concise solution using lists comprehension, but alas it eludes me
Similar solution in SML:
fun intersect (xs, ys) = let
fun loop(out, _, []) = List.rev out
| loop(out, [], _) = List.rev out
| loop(out,left as x::xs, right as y::ys) =
if x < y then loop(out, xs, right)
else if y < x then loop(out, left, ys)
else loop(x::out, xs, ys)
in
loop([], xs, ys)
end
fun nintersect [] = []
| nintersect (xs::xss) = List.foldl intersect xs xss;
[/sourcecode]
Sorry, issue with formatting:
This one is like Scott’s – for each element in the first array, discarding lower elements in the other arrays, moving on to the next element if no match is found. A nice flourish is to use a sentinel element at the end of each array, when a match is found and it’s the sentinel element we break the loop. No other bounds checking is needed:
In Python. This works for an arbitrary number of arrays.
The Python standard library collections.Counter acts like a multi-set, making this problem a one-liner.
for(p=0;p<a.length;p++)
{
for(q=0;q<b.length;q++)
{
if(b[q]==a[p])
{
// System.out.println("matching values found in array a and b:::"+b[q]);
for(r=0;r<c.length;r++)
{
if(c[r]==b[q])
{
common[common_ite]=c[r];
System.out.println("common integer found:::"+common[common_ite]);
common_ite++;
break;
}
//ends for loop r….for c array
}
break;
//end if of b array…
}
//end for loop of q …for b array…
}
//ends main foor loop..of array a…
}
It can be made more efficient by using common array with length of shortest array…use of break is not encouraged…but its fast and in any interview..it maybe helpful in time constraint..
OK, here’s another one, same trick with sentinels. Iterate down all 3 lists, if the current element in list i is less than the current element in list i+1 (mod 3), skip it. If no element is skipped, they must all be the same. Possibly does more comparisons than my other solution, but at least uses INT_MAX properly:
Wrote my own Bag implementation in Python, for the heck of it. Although obviously I should have used collections.Counter :)