Skip to content

Instantly share code, notes, and snippets.

@lukmdo
Last active August 29, 2015 14:23
Show Gist options
  • Save lukmdo/479a87e40c17d2e0e21b to your computer and use it in GitHub Desktop.
Save lukmdo/479a87e40c17d2e0e21b to your computer and use it in GitHub Desktop.
"""
FIND MOST COMMON ELEMENT IN LIST
- what is "most common"
- restrictions
- list properties
- tests?
"""
def solution(items):
"""
- time complexity
- space complexity
- issues / improvements
"""
if not items:
return
counter = dict()
best = None
for item in items:
if item in counter:
counter[item] += 1
if counter[item] > counter[item]:
best = item
else:
counter[item] = 1
if counter[best] > len(items) // 2:
return best
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment