How to count duplicates in Ruby Arrays

六眼飞鱼酱① 提交于 2019-12-17 09:22:10

问题


How do you count duplicates in a ruby array?

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


回答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}



回答2:


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 } 



回答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}



回答4:


Simple.

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



回答5:


arr = %w( a b c d c b a )
# => ["a", "b", "c", "d", "c", "b", "a"]

arr.count('a')
# => 2



回答6:


Another way to count array duplicates is:

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

arr.group_by{|x| x}.map{|k,v| [k,v.count] }

result is

[[2, 4], [3, 2], [4, 1]]




回答7:


requires 1.8.7+ for group_by

ary = %w{a b c d a e f g a h i b}
ary.group_by{|elem| elem}.select{|key,val| val.length > 1}.map{|key,val| key}
# => ["a", "b"]

with 1.9+ this can be slightly simplified because Hash#select will return a hash.

ary.group_by{|elem| elem}.select{|key,val| val.length > 1}.keys
# => ["a", "b"]



回答8:


To count instances of a single element use inject

array.inject(0){|count,elem| elem == value ? count+1 : count}



回答9:


What about a grep?

arr = [1, 2, "Thanks", "You're welcome", "Thanks", "You're welcome", "Thanks", "You're welcome"]

arr.grep('Thanks').size # => 3



回答10:


I don't think there's a built-in method. If all you need is the total count of duplicates, you could take a.length - a.uniq.length. If you're looking for the count of a single particular element, try
a.select {|e| e == my_element}.length.




回答11:


Improving @Kim's answer:

arr = [1, 2, "a", "a", 4, "a", 2, 1]
Hash.new(0).tap { |h| arr.each { |v| h[v] += 1 } }
# => {1=>2, 2=>2, "a"=>3, 4=>1}



回答12:


Its Easy:

words = ["aa","bb","cc","bb","bb","cc"]

One line simple solution is:

words.each_with_object(Hash.new(0)) { |word,counts| counts[word] += 1 }

It works for me.

Thanks!!




回答13:


Another way to do it is to use each_with_object:

a = [ 1, 2, 3, 3, 4, 3]

hash = a.each_with_object({}) {|v, h|
  h[v] ||= 0
  h[v] += 1
}

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

This way, calling a non-existing key such as hash[5] will return nil instead of 0 with Kim's solution.




回答14:


I've used reduce/inject for this in the past, like the following

array = [1,5,4,3,1,5,6,8,8,8,9]
array.reduce (Hash.new(0)) {|counts, el| counts[el]+=1; counts}

produces

=> {1=>2, 5=>2, 4=>1, 3=>1, 6=>1, 8=>3, 9=>1}



回答15:


Ruby code to get the repeated elements in the array:

numbers = [1,2,3,1,2,0,8,9,0,1,2,3]
similar =  numbers.each_with_object([]) do |n, dups|
    dups << n if seen.include?(n)
    seen << n 
end
print "similar --> ", similar


来源:https://stackoverflow.com/questions/1765368/how-to-count-duplicates-in-ruby-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!