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

前端 未结 16 2148
执念已碎
执念已碎 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:38

    Algorithm delete_duplicates (a[1....n])

    //Remove duplicates from the given array

    //input parameters :a[1:n], an array of n elements

    {

    temp[1:n]; //an array of n elements

     temp[i]=a[i];for i=1 to n
    
         temp[i].value=a[i]
    
            temp[i].key=i
    

    *//based on 'value' sort the array temp.*

    //based on 'value' delete duplicate elements from temp.

    //based on 'key' sort the array temp.//construct an array p using temp.

    p[i]=temp[i].value
    
    return p
    

    In other of elements is maintained in the output array using the 'key'. Consider the key is of length O(n), the time taken for performing sorting on the key and value is O(nlogn). So the time taken to delete all duplicates from the array is O(nlogn).

提交回复
热议问题