The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
mode
Posted by: auser | February 16, 2010 @ 11:03pm
Ruby Code
[
Download
]
def mode(arr=[]) hsh = {} max = 0 arr.each do |item| # store the item in the array if, making certain to set it to one, if it's not in there hsh.has_key?(item) ? hsh[item] += 1 : hsh[item] = 1 end # sort the array sorted_array = hsh.sort{|a,b| b[1]<=>a[1]} # Get the max value max = sorted_array[0][1] mode_arry = [] # Count the array up until it's at it's max sorted_array.each do |arr| mode_arry << arr[0] if arr[1] >= max end mode_arry end p mode %w(a b a a b c dd e e e) # ["e", "a"] p mode %w(a b a a b c dd e e) # ["a"] p mode %w(snowboarding is more than fun. super fun.) # ["fun."]
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.70 / 8
60 Votes
http://codebin.yi.org/508
page generated in 0.01 seconds