How to count duplicates in Ruby Arrays

Simple.

arr = [2,3,4,3,2,67,2]
repeats = arr.length - arr.uniq.length
puts repeats

Another version of a hash with a key for each element in your array and value for the count of each element

a = [ 1, 2, 3, 3, 4, 3]
h = Hash.new(0)
a.each { | v | h.store(v, h[v]+1) }

# h = { 3=>3, 2=>1, 1=>1, 4=>1 } 

This will yield the duplicate elements as a hash with the number of occurences for each duplicate item. Let the code speak:

#!/usr/bin/env ruby

class Array
  # monkey-patched version
  def dup_hash
    inject(Hash.new(0)) { |h,e| h[e] += 1; h }.select { 
      |k,v| v > 1 }.inject({}) { |r, e| r[e.first] = e.last; r }
  end
end

# unmonkeey'd
def dup_hash(ary)
  ary.inject(Hash.new(0)) { |h,e| h[e] += 1; h }.select { 
    |_k,v| v > 1 }.inject({}) { |r, e| r[e.first] = e.last; r }
end

p dup_hash([1, 2, "a", "a", 4, "a", 2, 1])
# {"a"=>3, 1=>2, 2=>2}

p [1, 2, "Thanks", "You're welcome", "Thanks", 
  "You're welcome", "Thanks", "You're welcome"].dup_hash
# {"You're welcome"=>3, "Thanks"=>3}

Given:

arr = [ 1, 2, 3, 2, 4, 5, 3]

My favourite way of counting elements is:

counts = arr.group_by{|i| i}.map{|k,v| [k, v.count] }

# => [[1, 1], [2, 2], [3, 2], [4, 1], [5, 1]]

If you need a hash instead of an array:

Hash[*counts.flatten]

# => {1=>1, 2=>2, 3=>2, 4=>1, 5=>1}