How to determine if any value occurs more than twice in a list?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 How To Determine If Any Value Occurs More Than Twice In A List?
00:34 Answer 1 Score 1
00:59 Accepted Answer Score 4
01:18 Answer 3 Score 2
01:55 Thank you
--
Full question
https://stackoverflow.com/questions/5119...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #algorithm #list #count
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 How To Determine If Any Value Occurs More Than Twice In A List?
00:34 Answer 1 Score 1
00:59 Accepted Answer Score 4
01:18 Answer 3 Score 2
01:55 Thank you
--
Full question
https://stackoverflow.com/questions/5119...
--
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.