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

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

    If the order does not matter, you might want to try this algorithm written in Python:

    >>> array = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6]
    >>> unique = set(array)
    >>> list(unique)
    [1, 2, 3, 4, 5, 6]
    

提交回复
热议问题