How to count Ruby Array items
The following is a guest post by Rex Feng and originally appeared on his blog. Rex is currently a student a The Flatiron School. You can learn more about him here, or follow him on twitter here. While working in Ruby, I often find myself trying to get a count of the number of times each item appears in an […]
Reading Time 0 mins
While working in Ruby, I often find myself trying to get a count of the number of times each item appears in an array. Below, I’ll show you how to create a Hash that iterates through each item in the array and counts the number of occurrences.
We have an array of
animals that we want to count.
We can count them up by creating a new Hash.
Next, we want to iterate through the
animals Array and set the
animal_count Key as the animal name & the
animal_count Value as the counter.
animals, we’ve either created a new Key / Value pair in
animal_count or incremented the Value corresponding to the
Posted by Flatiron School / December 18, 2012
Learn to Code Python: Free Lesson for Beginners
What is the difference between a data analyst and a data scientist?
While data analyst and data scientist roles attract similar types of creative and logical people, their roles do have stark differences. Here’s our breakdown of the lines between these often mixed up roles.