Skip to content

Instantly share code, notes, and snippets.

@snoopsD
Last active October 30, 2019 10:24
Show Gist options
  • Select an option

  • Save snoopsD/1e7eed8ec66b60f84d2977a397345dad to your computer and use it in GitHub Desktop.

Select an option

Save snoopsD/1e7eed8ec66b60f84d2977a397345dad to your computer and use it in GitHub Desktop.
def frequency(arr)
count_elments = arr.inject(Hash.new(0)) { |total, e| total[e] += 1 ; total }.sort.sort_by { |k,v| -v }
count_elments.map { |el| el[0] }
end
def flatten(arr)
flatten_array ||= []
arr.each do |el|
if el.is_a? Array
flatten_array.concat(flatten(el))
else
flatten_array << el
end
end
flatten_array
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment