arr consisting of
n integers is given. The dominator of
arr is the value that occurs in more than half of the elements of
Write a function
dominator(arr) that returns the dominator of
arr. The function should return −1 if array does not have a dominator. All values in arr will be >=0.
For example, consider the array such that
arr = [3,4,3,2,3,1,3,3]
The dominator of arr is 3 because it occurs in 5 out of 8 elements of arr and 5 is more than half of 8.
dominator([3,4,3,2,3,1,3,3]) => 3
dominator([1,2,3,4,5]) => -1
Want to propose a challenge idea for a future post? Email email@example.com with your suggestions!