title: Counting the distinct elements in an array
body: <p>I have an array: </p>
  
  <pre><code>a = [1, 2, 3, 3, 6, 8, 1, 9]
  </code></pre>
  
  <p>I want to display each unique element value and its associated element count like this:</p>
  
  <pre><code>1: 2
  2: 1
  3: 2
  6: 1
  8: 1
  9: 1
  </code></pre>
  
  <p>So far I have:</p>
  
  <pre><code>a.sort.group_by { |x| x }
  {
    1 =&gt; [
      [0] 1,
      [1] 1
    ],
    2 =&gt; [
      [0] 2
    ],
    3 =&gt; [
      [0] 3,
      [1] 3
    ],
    6 =&gt; [
      [0] 6
    ],
    8 =&gt; [
      [0] 8
    ],
    9 =&gt; [
      [0] 9
    ]
  }
  </code></pre>
  
  <p>So each element of the Hash contains an array. I can use that array's count to get my answer, but I'm having trouble figuring out how to process the hash concisely.</p>
  
  <p>Is this a horrible implementation?</p>
  
  <pre><code>a.sort.group_by { |x| x }.each {|x| puts "#{x[0]} #{x[1].count}" }
  </code></pre>