Hi,
Here is the 3rd hint.
We've already created a hash, and used the each method to start the hash out with a key for each unique element of the hash, and we've already set the value for each key in the hash to be equal to 0.
h = Hash.new
a = [1, 2, 2, 3, 3, 1]
a.each { |e| h[e] = 0 }
puts h.inspect # {1 => 0, 2 => 0, 3 => 0}
Now, we are ready to count how many times each element in the array actually occurs.
a.each { |e| h[e] += 1 }
puts h.inspect # {1 => 2, 2 => 2, 3 => 2}
We are half way there. We have a hash that already has the information about which element or elements occur the most. Remember that the goal of the method is to return an array. How can we return the desired array from the hash? I will post the answer later today. But here's a clue: hashes have methods that allow you to get information from them. Take a look at the methods in the Hash object to see if there are any that will help us. You can see what methods are in hash objects by creating an empty hash and calling the methods method on it.
Glenn
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment