1. Use set in case of hash. the time complexity is O (n)
return list(set(s))
2. hash is not supported, but sort is supported. The time complexity is O (nlogn)
t=list(s)try: t.sort()except TypeError: del telse: return [x for i,x in enumerate(t) if not i or t[i]!=t[i-1]]
3. If neither of the preceding two is possible, use in to determine the time complexity. The time complexity is O (n ** 2)
u=[]for x in s: if x not in u: u.append(x)return u
Note:
-
If an object has a hash value (that is, the hash () method) that remains unchanged within its lifecycle, its hashable and variable objects such as list cannot be hashed.
-
The hashable object has the _ eq _ () and _ cmp _ () methods. The two objects with the same hash value are also equal.
-
The keys in the dictionary and the elements in the set must be hash-able, because their hash values are used inside the data structure.
-
If a class does not define the cmp and eq methods, it cannot define the hash operation. If the first two hash methods are defined, their instances cannot be used in the sequence in which the hash is required.
-
A variable class cannot define the hash method, even if it defines the cmp and eq methods.
-
The user-defined class has the _ cmp _ () and _ eq _ () methods by default. Its objects are equal only when compared with their own, x. _ hash _ () returns id (x)
-
Non-hash class can display definition _ hash __= None