defunique(s): ''' for best speed, all sequence elements should be hashble. Then unique() will usually work in linear time. if not possible, the swquenct elements should enjoy a total ordering, and if `list(s).sort()` doesn't raise TypeError, it is assumed that they do enjoy a total ordering. Then unique() will usually work in O(N*logN) time. If that's not possible either, the sequence elements must support equality-testing. Then unique() will usually work in quadratic time. ''' n = len(s) if n == 0: return []
# try using a dict first, as that is the fastest and usually work u = {} try: for x in s: u[x] = 1 except TypeError: del u # move on to the next method else: return u.keys()
try: t = list(s) t.sort() except TypeError: del t # move on to the next method else: assert n > 0 last = t[0] lasti = i = 1 while i < n: if t[i] != last: t[lasti] = last = t[i] lasti += 1 i += 1 return t[:lasti]
u = [] for x in s: if x notin u: u.append(x) return u