ruby: how to find non-unique elements in array and print each with number of occurrences?
arrays, ruby
Solution
puts a.uniq.
map { | e | [a.count(e), e] }.
select { | c, _ | c > 1 }.
sort.reverse.
map { | c, e | "#{e}:#{c}" }
Problem
I have ``` a = ["a", "d", "c", "b", "b", "c", "c"] ``` and need to print something like (sorted descending by number of occurrences): ``` c:3 b:2 ``` I understand first part (finding NON-unique) is: ``` b = a.select{ |e| a.count(e) > 1 } => ["c", "b", "b", "c", "c"] ``` or ``` puts b.select{|e, c| [e, a.count(e)] }.uniq c b ``` How to output each non-unique with number of occurrences sorted backwards?