Skip to content

Instantly share code, notes, and snippets.

@nclv
Created February 10, 2020 07:05
Show Gist options
  • Save nclv/8b8ec67af58431c9166b0470c4fb873b to your computer and use it in GitHub Desktop.
Save nclv/8b8ec67af58431c9166b0470c4fb873b to your computer and use it in GitHub Desktop.
from more_itertools import ilen
def nombre_occurrences(liste, element):
return ilen(filter(lambda el: el == element, liste))
def element_maj(liste):
return next(filter(lambda element: nombre_occurrences(liste, element) > len(liste)//2, liste), None)
compte = defaultdict(int)
foe element in liste:
compte[element] += 1
maj = max(compte.keys(); key=lambda k: compte[k])
if compte[maj] > len(liste):
return maj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment