title: frequency of objects in an array using Ruby
body: <p>If i had a list of balls each of which has a color property. how can i cleanly get the list of balls with the most frequent color.</p>
  
  <pre><code>[m1,m2,m3,m4]
  </code></pre>
  
  <p>say,</p>
  
  <pre><code>        m1.color = blue
          m2.color = blue
          m3.color = red
          m4.color = blue
  </code></pre>
  
  <p><code>[m1,m2,m4]</code> is the list of balls with the most frequent color</p>
  
  <p>My Approach is to do:</p>
  
  <pre><code>[m1,m2,m3,m4].group_by{|ball| ball.color}.each do |samecolor|
    my_items = samecolor.count
  end
  </code></pre>
  
  <p>where count is defined as</p>
  
  <pre><code>class Array
    def count
    k =Hash.new(0)
    self.each{|x|k[x]+=1}
    k
    end
  end
  </code></pre>
  
  <p>my_items will be a hash of frequencies foreach same color group. My implementation could be buggy and i feel there must be a better and more smarter way. 
  any ideas please?</p>