Created
March 27, 2021 20:29
-
-
Save Clivern/20f137f314ea4f97e15991d296194333 to your computer and use it in GitHub Desktop.
Gets the index of the dominator of array A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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