How to determine if any value occurs more than twice in a list?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:59 Accepted answer (Score 4)
01:22 Answer 2 (Score 2)
02:13 Answer 3 (Score 1)
02:45 Thank you
--
Full question
https://stackoverflow.com/questions/5119...
Accepted answer links:
[collections.Counter]: https://docs.python.org/2/library/collec...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #algorithm #list #count
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:59 Accepted answer (Score 4)
01:22 Answer 2 (Score 2)
02:13 Answer 3 (Score 1)
02:45 Thank you
--
Full question
https://stackoverflow.com/questions/5119...
Accepted answer links:
[collections.Counter]: https://docs.python.org/2/library/collec...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #algorithm #list #count
#avk47
ACCEPTED ANSWER
Score 4
You can use collections.Counter for this:
from collections import Counter
print any(count > 2 for count in Counter(myArray).itervalues()) # True
Or, if you are using Python 3:
from collections import Counter
print(any(count > 2 for count in Counter(myArray).values())) # True
ANSWER 2
Score 2
Here's one way using collections.defaultdict. Like @HoriaComan's method, this solution doesn't require iterating your entire list.
myArray = [1,1,1,1,1,1,1,1,2]
from collections import defaultdict
def count_limit(L, k=2):
d = defaultdict(int)
for item in L:
if d[item] == k:
return True
else:
d[item] += 1
return False
res = count_limit(myArray) # False
Performance benchmarking
To demonstrate the impact, we can compare with Counter on a larger iterable:
myArray = [1,1,1,1,1,1,1,1,2]*10000
from collections import defaultdict, Counter
def count_limit(L, k=2):
d = defaultdict(int)
for item in L:
if d[item] == k:
return True
else:
d[item] += 1
return False
def count_limit_counter(myArray):
return any(count > 2 for count in Counter(myArray).values())
%timeit count_limit(myArray) # 1.52 µs per loop
%timeit count_limit_counter(myArray) # 6.64 ms per loop
ANSWER 3
Score 1
You could always construct a histogram of the values and see if any entry is greater than two. It could look something like this:
def is_more_than_twice(l):
hist = {}
for el in l:
if el in hist:
hist[el] += 1
else:
hist[el] = 1
if hist[el] > 2:
return True
return False
You don't need to iterate until the end of the list, just until you've met the condition of seeing an element el appearing more than twice.