I have
a = [\"a\", \"d\", \"c\", \"b\", \"b\", \"c\", \"c\"]
and need to print something like (sorted descending by number of occurrences)
How about:
a.sort.chunk{|x| a.count(x)}.sort.reverse.each do |n, v| puts "#{v[0]}:#{n}" if n > 1 end
I personally like this solution:
a.inject({}) {|hash, val| hash[val] ||= 0; hash[val] += 1; hash}. reject{|key, value| value == 1}.sort.reverse. each_pair{|k,v| puts("#{k}:#{v}")}