title: Groovy List : Group By element's count and find highest frequency elements
body: <p>I have a groovy list as below</p>
  
  <pre><code>def certs = ['0xc1','0xc1','0xc1','0xc1','0xc2','0xc2','0xc3','0xc4','0xc4','0xc5','0xc5','0xc5','0xc5']
  </code></pre>
  
  <p>Am trying to find the occurance of each element and group by its count. 
  I've tried </p>
  
  <pre><code>certs.groupBy { it }.findAll { it.value.size() }
  </code></pre>
  
  <p>but am getting the below output</p>
  
  <pre><code>[0xc1:[0xc1, 0xc1, 0xc1, 0xc1], 0xc2:[0xc2, 0xc2], 0xc3:[0xc3], 0xc4:[0xc4, 0xc4], 0xc5:[0xc5, 0xc5, 0xc5, 0xc5]]
  </code></pre>
  
  <p>Instead am expecting below</p>
  
  <pre><code>[0xc1:4, 0xc2:2, 0xc3:1, 0xc4:2, 0xc5:4]
  </code></pre>
  
  <p>can someone help me with this? Also I wanna find maximum occurring element in the list in my case its <code>0xc1</code> and <code>0xc5</code> </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <pre><code>def myMap = certs.inject([:]) { m, x -&gt; if (!m[x]) m[x] = 0; m[x] += 1; m }
  def maxValue = myMap.values().max{it} 
  def myKeys = []
  myMap.findAll{ it.value == maxValue }.each{myKeys &lt;&lt; it?.key}
  println myKeys  // result = [0xc1:4, 0xc5:4]
  //println myMap.sort { a, b -&gt; b.value &lt;=&gt; a.value }
  </code></pre>