rubyarrayscountduplicate-data

How to count duplicates in Ruby Arrays


How do you count duplicates in a ruby array?

For example, if my array had three a's, how could I count that


Solution

  • 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}