Algorithm - How to delete duplicate elements in a list efficiently?

前端 未结 16 2167
执念已碎
执念已碎 2020-12-01 04:21

There is a list L. It contains elements of arbitrary type each. How to delete all duplicate elements in such list efficiently? ORDE

16条回答
  •  一个人的身影
    2020-12-01 04:36

    My code in Java:

    ArrayList list = new ArrayList();
    
    list.addAll({1,2,1,3,4,5,2,3,4,3});
    
    for (int i=0; i

    or simply do this:

    SetList unique = new SetList();
    
    unique.addAll(list);
    

    Both ways have Time = nk ~ O(n^2)

    where n is the size of input list,

    k is number of unique members of the input list

提交回复
热议问题