Hi,
Here's the 4th and last hint.
So far, we have:
h = Hash.new
a = [1, 2, 2, 3, 3, 1]
a.each { |e| h[e] = 0 }
a.each { |e| h[e] += 1 }
That gives us a hash with a key for each element in the original array, and value for each time that key appeared in the array.
To complete the assignment you need to do 5 things:
1) Find out what the maximum times an element appeared in the array using the values method on the hash, which returns an array.
2) Find out the highest value with the max method on that array.
3) Remove all of the key-value pairs in the hash that do not have a value equal to the highest value.
4) Use the keys method on the hash that's left.
5) So far, none of the code that I've shown has been in a method definition. So the last step would be to take what we have and write a method definition around it.
There's a couple of things that I've shown here that could be made more efficient and elegant. Take a minute to look at the code to see if you can come up with what they are.
See you on Monday!
Glenn
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment