I have this array, for example (the size is variable):
x = ["1.111", "1.122", "1.250", "1.111"]
and I need to find the most commom value ("1.111" in this case).
Is there an easy way to do that?
Tks in advance!
EDIT #1: Thank you all for the answers!
EDIT #2: I’ve changed my accepted answer based on Z.E.D.’s information. Thank you all again!
Ruby < 2.2
Note:
Enumberable.max_byis new with Ruby 1.9, but it has been backported to 1.8.7Ruby >= 2.2
Ruby 2.2 introduces the Object#itself method, with which we can make the code more concise:
As a monkey patch
Or as
Enumerable#mode: