Skip to content

Instantly share code, notes, and snippets.

@Clivern
Created March 27, 2021 20:29
Show Gist options
  • Save Clivern/20f137f314ea4f97e15991d296194333 to your computer and use it in GitHub Desktop.
Save Clivern/20f137f314ea4f97e15991d296194333 to your computer and use it in GitHub Desktop.
Gets the index of the dominator of array A
from collections import Counter
def solution(A):
"""Gets the index of the dominator of array A"""
counter = Counter()
for i, v in enumerate(A):
counter[v] += 1
counter = counter.most_common()
if len(counter) == 0:
return -1
if len(counter) == 1:
return A.index(counter[0][0])
elif counter[0][1] > counter[1][1]:
return A.index(counter[0][0])
return -1
if __name__ == '__main__':
assert solution([2, 2, 3, 3, 1]) == -1
assert solution([2, 2, 3, 1]) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment