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

我正在寻找类似的东西

result1 = sample1.magik_function
=>"b"
4

1 回答 1

1
sample1.max_by{|c| total.count(c)} # => "b"
sample2.max_by{|c| total.count(c)} # => "a"
于 2013-05-26T22:34:09.867 回答