Array remove duplicate elements

前端 未结 12 1404
陌清茗
陌清茗 2020-11-27 16:17

I have an unsorted array, what is the best method to remove all the duplicates of an element if present?

e.g:

a[1,5,2,6,8,9,1,1,10,3,2,4,1,3,11,3]
         


        
12条回答
  •  有刺的猬
    2020-11-27 16:37

    You can use the "in" and "not in" syntax in python which makes it pretty straight forward.

    The complexity is higher than the hashing approach though since a "not in" is equivalent to a linear traversal to find out whether that entry exists or not.

    li = map(int, raw_input().split(","))
    a = []
    for i in li:
        if i not in a:
            a.append(i)
    print a
    

提交回复
热议问题