Skip to content

Instantly share code, notes, and snippets.

@maraca
Created August 10, 2011 19:18
Show Gist options
  • Select an option

  • Save maraca/1137868 to your computer and use it in GitHub Desktop.

Select an option

Save maraca/1137868 to your computer and use it in GitHub Desktop.
def arr_leader(values):
storage = {}
max_sum_key = values[0]
max_sum_val = 1
for i in values:
if storage.get(i) is None:
storage[i] = 0
storage[i] += 1
# stores the current highest sum
if storage[i] > max_sum_val:
max_sum_key = i
max_sum_val = storage[i]
if max_sum_val > len(values) / 2:
return max_sum_key
return -1
if __name__ == '__main__':
print arr_leader([1, 2, 3 , 5, 6 ,7, 3, 3 ,3 , 3, 3 ,3 ,3 ,3 ])
print arr_leader([1, 2, 3 ,4 ])
#############################
> python2.6 ten_minutes.py
3
-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment