We have three lists which contain people’s names.
All three lists have been sorted alphabetically.
Now we need to find at least one name which appear in all three lists.
The algorithm I am thinking is like this:
I get three heads out of three lists.
if the three heads are not equal to each other, then I keep the max
one and get two new heads from the lists from which I just dropped the
heads.Continue above procedure until I find such an element as described in
the beginning.
Is this algorithm correct?
The problem is that I am not sure how to use ocaml to write the function.
Thomash’s algorithm will do the job with two calls of
intersectand creating intermediate lists so it isn’t very efficient.Your algorithm is essentially correct. An extra bit is that sometimes you have two heads are equal to max and you should drop only the remaining head.
Here is the revised algorithm written in OCaml: