The solution you show is also ineffective as the methods
remove
and
contain
works at the speed of O(n). (For understanding "Big O Notation", see
http://en.wikipedia.org/wiki/Big_O_notation[
^].)
Speed considerations and the no-duplicate constrain suggest the use of
java.util.Hashtable
, see
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html[
^] at the expense of some memory overhead. For big collections, this class provides the speed about O(1) (no dependency on the size of collection, constant search time for big collections).
You can populate an instance of hash table from one list and try to find the elements of another list one by one. The rest of this algorithm is simple, so I would leave to for your home exercise :-).
—SA