Q:

Find all max of elements of an array

Suppose I have a array, namely arr: [1, 2, 3, 4, 8, 8], and I want to find all max elements in this array:
arr.allmax # => [8, 8]

Is there a built-in method combinations to solve this? I don't like to monkey patch as I am doing now:
class Array
  def allmax
    max = self.max
    self.select { |e| e == max }
  end
end

Monkey patch is not a good idea, I could just do:
some_array.select { |e| e == some_array.max }

and it will work as allmax. Thanks for all answers and comments for inspirations.

A:

Here's a fun way to do it.
arr.sort!.slice arr.index(arr[-1]) || 0..-1

Sort the array, then find the leftmost index of the array which matches the rightmost index of the array, and take the subslice that matches that range (or the range 0..-1 if the array is empty).
This one is kind of fun in that it requires no intermediate arrays, though it does mutate the input to achieve the one-liner.

