Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the runtime complexity of this program?(esp retainAll() method)

Tags:

java

list

set

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?

like image 890
Yizhang Shen Avatar asked Dec 07 '25 03:12

Yizhang Shen


1 Answers

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

like image 116
PSR Avatar answered Dec 08 '25 16:12

PSR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!