public static List<Integer> returnIntersection(List<Integer> a,List<Integer> b){
List<Integer> l1=new ArrayList<Integer>(a);
List<Integer> l2=new ArrayList<Integer>(b);
l1.retainAll(l2);//find intersection in l2
l1=removeDuplicates(l1);
return l1;}
public static List<Integer> removeDuplicates(List<Integer> l) {
Set<Integer> se=new HashSet<Integer>(l);
l.clear();
l=new ArrayList<Integer>(se);
return l;}
The code above is to return a list containing intersection of 2 lists without duplicates.My question is what is time complexity of this? What is time complexity of the retainAll() method? And is there any time consuming while turning lists into sets?
An interesting topic is to measure the complexity of individual methods. There are several things that contribute to the complexity.
see here how to measure complexity
It is very good site how to calculate complexity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With