February 13 - Build a Histogram of Integer Counts in Ruby
This guide examines how to build a histogram in Ruby that takes in an Array of integers and returns a Hash that counts the number of occurrences of each integer.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

Summary

Build a histogram in Ruby that returns a hash.

Exercise Description

Given the following array of integers:

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

Build out a method that generates a histogram in hash form where the key is the integer that is being counted, and the value is the number of occurrences.

Expected Output

{
  1=>3,
  4=>5,
  3=>1,
  2=>1,
  5=>2
}

Real World Usage

Counting the number of occurrences of a value in a collection is a common practice. Imagine that you need to calculate the number of times that a user has commented on a friend's picture in a social media application.

Test Cases

Code File