The edge cases made this one tricky. Here's a working function in Ruby without using Tally
defdominator(arr)baseline=arr.length/2# Tally how many times each `n` is in the array# Sort by value# Delete any entries where the value is less than or equal to the baseline (half the length of the array)# Put values in order, largest to smallest# Turn it back into a hashresult=arr.inject(Hash.new(0)){|memo,item|memo[item]+=1;memo}.sort_by{|key,value|value}.delete_if{|key,value|value<=baseline}.reverse.to_hresult!={}?result.values.first:-1end
The edge cases made this one tricky. Here's a working function in Ruby without using Tally
Results