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

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

    • go through the list and assign sequential index to each item
    • sort the list basing on some comparison function for elements
    • remove duplicates
    • sort the list basing on assigned indices

    for simplicity indices for items may be stored in something like std::map

    looks like O(n*log n) if I haven't missed anything

提交回复
热议问题