Better way to count the number of occurrences of a maximum number in the array in Ruby I am trying to write a program to get the count of the maximum number in an array of integers. The below code is what I have written. Is there any better way to write this code in Ruby?
def max_number_count(my_array)
    max_num, max_num_count = my_array.inject([0, 0]) { 
                               |arr, age| age &gt; arr[0] ? 
                               [arr[0] = age, arr[1] = 1] : age == arr[0] ?
                               arr[1] += 1 : arr[1] = arr[1]; 
                               arr 
                             }
    max_num_count
end

max_number_count([1,2,3,3,3])
#3
```
Your method shouldn't call the given array my_array. It's not the method's array, it's the caller's array. Just call it array, which is also shorter, so less clutter.
And you're making it very complicated. The array already offers all you need:
def max_number_count(array)
  array.count(array.max)
end

That's also much faster:
            user     system      total        real
yours   1.696091   0.034977   1.731068 (  3.476805)
mine    0.074057   0.000000   0.074057 (  0.131310)

Benchmark code:
require 'benchmark'

def max_number_count1(my_array)
    max_num, max_num_count = my_array.inject([0, 0]) { 
                               |arr, age| age &gt; arr[0] ? 
                               [arr[0] = age, arr[1] = 1] : age == arr[0] ?
                               arr[1] += 1 : arr[1] = arr[1]; 
                               arr 
                             }
    max_num_count
end

def max_number_count2(array)
  array.count(array.max)
end

n = 5
array = (1..1000).to_a * 1000
Benchmark.bmbm do |x|
  x.report(&quot;yours&quot;) { n.times { max_number_count1(array) } }
  x.report(&quot;mine&quot;)  { n.times { max_number_count2(array) } }
end