I have 2 lists of integers,
l1 = new ArrayList();
l2 = new ArrayList();
I want to find out duplicate items in both of them, I have my usual approach:-
for (Integer i : l1)
{
if(l2.contains(i)){
System.out.println("Found!");
}
}
I've heard contains()
is O(n)
, making my implementation O(n^2)
.
Is there a better way to do this, (less than O(n^2)
) ?