Skip to content

Latest commit

 

History

History
12 lines (11 loc) · 255 Bytes

File metadata and controls

12 lines (11 loc) · 255 Bytes

Group and count

def group_and_count arr, sort_by_value=true
  h = arr.inject(Hash.new(0)) { |h, e| h[e] += 1 ; h }
  if sort_by_value
    return h.sort_by {|_key, value| value}.to_h.to_json
  else
    return h.sort.to_h.to_json
  end
end