Skip to content

Instantly share code, notes, and snippets.

@yuheiomori
Created August 10, 2014 10:46
Show Gist options
  • Save yuheiomori/95960705119b4b25c5f2 to your computer and use it in GitHub Desktop.
Save yuheiomori/95960705119b4b25c5f2 to your computer and use it in GitHub Desktop.
The Major Element(CodeEval) in Python 3.x
# coding=utf-8
import sys
def major_element(elements):
m = dict()
for e in elements:
m.setdefault(e, 0)
m[e] += 1
if m[e] > len(elements) / 2:
return e
return None
def main():
with open(sys.argv[1], "r") as f:
for line in f:
elements = line.rstrip().split(',')
print(major_element(elements))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment