fork download
  1. def mode(array)
  2. p array
  3. hash = Hash.new(0)
  4. array.each do |i|
  5. hash[i]+=1
  6. end
  7. p hash
  8. to_a = hash.to_a
  9. p to_a
  10. sorted = to_a.sort { |k,(a,b)| a <=> b }
  11. p sorted
  12. last = sorted.last
  13. p last
  14. end
  15.  
  16. mode(["a", "b", "c", "c", "a"])
Runtime error #stdin #stdout #stderr 0.01s 7468KB
stdin
Standard input is empty
stdout
["a", "b", "c", "c", "a"]
{"a"=>2, "b"=>1, "c"=>2}
[["a", 2], ["b", 1], ["c", 2]]
stderr
prog.rb:10:in `sort': comparison of Array with Array failed (ArgumentError)
	from prog.rb:10:in `mode'
	from prog.rb:16:in `<main>'