Skip to content

Instantly share code, notes, and snippets.

@AlexMikhalev
Created January 17, 2016 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexMikhalev/498313db3f57e6a47045 to your computer and use it in GitHub Desktop.
Save AlexMikhalev/498313db3f57e6a47045 to your computer and use it in GitHub Desktop.
from collections import defaultdict
from operator import itemgetter
def solution(A):
# write your code in Python 2.7
appearances = defaultdict(int)
N=len(A)
for each_key in A:
appearances[each_key] += 1
if not appearances:
return -1
max_elem=max(appearances.iteritems(), key=itemgetter(1))
if max_elem[1]>(N/2):
for k,v in enumerate(A):
if v==max_elem[0]:
return k
else:
return -1
if __name__ == '__main__':
print 'Start tests..'
assert solution(A) in [0, 2, 4, 6, 7]
assert solution([2,1,3,1,4,1,5,1])==-1
assert solution([]) == -1
print 'Ok!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment