Best algorithm for delete duplicates in array of strings

前端 未结 7 1896
一个人的身影
一个人的身影 2021-02-09 02:44

Today at school the teacher asked us to implement a duplicate-deletion algorithm. It\'s not that difficult, and everyone came up with the following solution (pseudocode):

<
7条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 03:11

    def dedup(l):
        ht, et = [(None, None) for _ in range(len(l))], []
        for e in l:
            h, n = hash(e), h % len(ht)
            while True:
                if ht[n][0] is None:
                    et.append(e)
                    ht[n] = h, len(et) - 1
                if ht[n][0] == h and et[ht[n][1]] == e:
                    break
                if (n := n + 1) == len(ht):
                    n = 0
        return et
    

提交回复
热议问题