2013-05-26 2 views
0
total =["a","a","a","b","b","b","b","b","b","b","b","c","c","c","d","d","d","d","d","d","d","d","b","b","e","e","e","e","e","f"] 

smart_total = total.group_by{|i| i}.map{|i,j| [i,j.length]} 
smart_total = [["a", 3], ["b", 10], ["c", 3], ["d", 8], ["e", 5], ["f", 1]] 

내가 최대 빈도 항목에 따라 샘플 배열빈번한 항목 필터 사용 루비

sample1 = ["a","f","b"] 

를 가지고 최종 출력 가지는 하나 개의 원소, 여기서 그 b

sample2 = ["a","c"] 

출력은 a 또는 c

입니다. m은

result1 = sample1.magik_function 
=>"b" 

답변

1
sample1.max_by{|c| total.count(c)} # => "b" 
sample2.max_by{|c| total.count(c)} # => "a" 
+0

완벽한처럼 뭔가를 찾고! 고맙습니다! – gkolan