Two Ways of Finding the Element That Occurs the Most in an Array with Ruby.

You know I used a cute bunny as my blog photo.

Two Ways of Finding the Element That Occurs the Most in an Array with Ruby.

dallas billeBlockedUnblockFollowFollowingFeb 20In my post I will discuss a specific task operated on an array, that to me is a bit interesting and I like it, and also exemplifies the Ruby idea that there are multiple ways of performing one task, and a single one isn’t considered “right”.

Isn’t that a wonderfully accepting outlook?Anyway, multiple times I have come across a task which required me to iterate over an array, with the purpose of attempting to select the element that occurs the most in that array.

And I will narrate a delightful example.

Below is an example:Carol is an animal hoarder.

She just cant help herself.

Any stray she comes across she has to take home.

It’s a real problem.

It becomes such a problem that animal control has told her she needs to document what pets has, because she has to get rid of some of these animals.

She lives in a one bedroom apartment for god’s sake.

She decides she will get rid of one of the pets she has the most of, but there are so many, she doesn’t want to count them by hand.

Here is the array of Carols pets:pets = [ “dog”, “dog”, “cat”, “bird”, “chinchilla”, “rat”, “hampster”, “dog”, “rat”, “bird”, “cat”, “giraffe”, “cat”, “bird”,”dog”, “snake”, “hamster”, “dog” , “mouse”, “spider”, “dog”, “cat”]Method 1:The first method I will use, converts the array into a hash with values.

We assume you understand how to set a hashes keys and values.

What this method does is iterates over the array, sets an element of the array as a key the hash, and for each elements’ occurrence, increases its value by one.

So if it comes across a pet that exists already in the hash, it increases that pets’ value by one, while keeping the key unique.

Here is what the first method looks like:pet_count = Hash.


each {|pet| pet_count[pet] += 1}pet_count.

sort_by { |k,v| v }.

lastLet’s break it down a little bit.

pet_count = Hash.

new(0)#creates a new hash, sets its default values to 0.


each {|pet| pet_count[pet] += 1}#iterates each element, sets our empty hashes key to that of a pet.

# “+=“ is the operator on the value of a keys occurrence in the hash.


first iteration: pet_count = {“dog” => 1 }, this is the first occurrence of “dog”).

If our second iteration contains the same element of a key in our hash, then it adds 1 to that keys’ value, while keeping the key unique, which is what happens.


second iteration: pet_count = {“dog” => 2}, second occurrence of “dog”).

Our third iteration sets a new key and a new value, since the next element in our carols_pets array is new to our hash.

Our hash doesn’t contain a key of “cat”, a new key is created, and a value automatically set to 1.

Now our pet_count = {“dog”=>2, “cat”=> 1}.

To me this is beautiful.

It touches on iteration and the structure of hash creation with key and value setting in a block.

If you are new to operating on arrays and hashes, this is a good method to dissect.

It’s also satisfying to view the result.

Which is this!pet_count = {“dog"=>6, "cat"=>3, "bird"=>3, "chinchilla"=>1, "rat"=>2, "hamster"=>2, "snake"=>1, "mouse"=>1, "spider"=>1}As you can see our hash is not quite in order.

Now we can operate on this hash to return the most common occurring pet, using the method below.


sort_by{|pet,number| number}.

last[0]#hash gets sorted by numberNow lets break this down a little bit:.

sort_by is a hash method, and the block is what the hash gets sorted by(number of times a pet occurs).

If we just ran:pet_count.

sort_by {|pet,number| number}#returns [["snake", 1], ["spider", 1], ["mouse", 1], ["chinchilla", 1], ["rat", 2], ["hamster", 2], ["bird", 3], ["cat", 3], ["dog", 6]]The default sorting with this method for integers is low to high, so actually the last key-value pair is the most commonly occurring.

So all you have to do is call the method “last”.


sort_by{|pet,number| number}.

lastreturns [“dog”=> 6]All you have to do is add on [0], which operates on an array index number.


sort_by{|pet,occurrences| occurrences}.

last[0]returns "dog"There we go.

We have our most popular pet.

It’s a little lengthy and complicated though.

Method 2:This one is significantly shorter, spits out the exact answer you need, and doesn’t require you to convert the array into a hash.

Which is nice as well.

And I would likely use this from here on out, if all I wanted was return the pet that occurred the most.

Remember our array of Carols pets:pets = [ “dog”, “dog”, “cat”, “bird”, “chinchilla”, “rat”, “hamster”, “dog”, “rat”, “bird”, “cat”, “giraffe”, “cat”, “bird”,”dog”, “snake”, “hamster”, “dog” , “mouse”, “spider”, “dog”, “cat”]Here is our method:pets.

max_by { |i| pets.

count(i)}What is going on here?.A method inside a method.

The inception of methods.

What .

max_by does:Returns the Object for the maximum value.

If we wanted to check to see which animal had the longest name, we could use:pets.

max_by{|pet| pet.

length}returns "chinchilla"Which is the pet with the longest name.

You’ll notice that:chinchilla.

length = 10.

max_by returns the object.

What .

count does:Pretty self explanatory, it counts items in the array.

If we did :pets.

count = 20It would return the number of items in the array, 20.

If we gave it an argument, like:pets(“cat”)pets(“cat”) = 4It returns the number of times cat shows up in the array.

When we use:pets.

max_by{|pet| pets.

count(pet)} we are simply combining these methods.

We know a pet object will be returned, with max_by, and each pet in the array is being counted with pets.


Each element is being iterated and counted, then max_by picks out the object that has the highest count.

Which is “dog”.

pets = [ “dog”, “dog”, “cat”, “bird”, “chinchilla”, “rat”, “hamster”, “dog”, “rat”, “bird”, “cat”, “giraffe”, “cat”, “bird”,”dog”, “snake”, “hamster”, “dog” , “mouse”, “spider”, “dog”, “cat”]pets.

max_by {|i| pets.

count(i)}returns "dog"Conclusion:Each method has its own benefits.

Say you wanted more information, like how many of the most popular pet there is, the Hash method is a good bet.

If you specifically want the return to be the pet, and have no use for any additional information, the second method is the way to go.

Just a nice single line.

Here may be some helpful resources if you are starting out.

Get used to reading array and hash methods with Ruby-Doc.

orgRuby Doc Array MethodsRuby Doc Hash MethodsWorking with HashesMore Ways!.. More details

Leave a Reply