Answers for "ruby count same string in array"

0

ruby get the number of same element in array

names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
counts = Hash.new(0)
names.each { |name| counts[name] += 1 }
# => {"Jason" => 2, "Teresa" => 1, ....
Posted by: Guest on March-13-2021
0

ruby get the number of same element in array

names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
counts = names.each_with_object(Hash.new(0)){ |string, hash| hash[string] += 1 }
puts counts # => { Jason: 2, Teresa: 1, Judah: 3, ... }
Posted by: Guest on March-13-2021

Code answers related to "ruby count same string in array"

Browse Popular Code Answers by Language