sorting by frequency of occurrence in an array

Is there an efficient way of doing this. I have an array

a=[1,2,2,3,1,2]

I want to output the frequency of occurrence in an ascending order. Example

[[3,1],[1,2],[2,3]]

Here is my code in ruby.

b=a.group_by{|x| x}
out={}

b.each do |k,v|
    out[k]=v.size
end

out.sort_by{|k,v| v}

Answers


a = [1,2,2,3,1,2]
a.each_with_object(Hash.new(0)){ |m,h| h[m] += 1 }.sort_by{ |k,v| v }
#=> [[3, 1], [1, 2], [2, 3]]

Need Your Help

Transactions in JSF Netbeans 7.3.1 generated code

jsf netbeans facade generated-code

I'm new to Java EE, following the learning by doing approach. In a very simple Mysql database with Netbeans 7.3.1 having a Patient table relates to a Person table, I'm having trouble understanding

update fields of conditionally rendered form

jsf-2 primefaces myfaces

my problem is, that I want to render form2 conditionally. This collides with a ajax update event of another form1, which should trigger the rendering of form2.