Answers for "check for same values in an array in ruby"

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 "check for same values in an array in ruby"

Browse Popular Code Answers by Language