[Tutor] Testing if a number occurs more than once [profiling
a function using profile.run()]
alan.gauld@bt.com
alan.gauld@bt.com
Tue Dec 3 05:44:02 2002
> > def morethanone(l):
> > for n in l:
Doh! My previous reply missed the loop. I'm obviously not
concentrating these days - trying to read too many digests
at once...
To try to make ammends I'll suggest an alternative approach
to the ones I've seen so far.
Create a dictionary then check for any values higher than 1.
It effectively gives a O(2n) solution rather than O(n**2)...
def morethanone(L):
d = {}
for i in L:
try: d[i] += 1
except: d[i] = 1
for i in d.keys():
if d[i] > 1: return 1
return 0
Apologies everyone,
Alan g.