Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

Here is one way :

2.1.0 :006 > arr = [1, 2, 3, 4, 8, 8]
 => [1, 2, 3, 4, 8, 8] 
2.1.0 :007 > arr.group_by { |i| i }.max.last
 => [8, 8] 
2.1.0 :008 > 

Here is a method :-

  def all_max(arr)
    return [] if arr.empty?
    arr.group_by { |i| i }.max.last
  end

Another way:

def all_max(arr)
  return [] if arr.empty?
  mx = arr.max
  [mx] * arr.count { |e| e == mx }
end

all_max([1, 2, 3, 4, 8, 8]) 
  #=> [8, 8]

To construct the array in a single pass, you could do this:

arr.each_with_object([]) do |e,a|
  if a.empty?
    a << e
  else
    case e <=> a.first
    when 0 then a << e
    when 1 then a.replace([e])
    end
  end
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top