Skip to content

Instantly share code, notes, and snippets.

@aarkerio
Created May 18, 2021 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarkerio/3f0a47f2f4a43b0c5534363396c5c707 to your computer and use it in GitHub Desktop.
Save aarkerio/3f0a47f2f4a43b0c5534363396c5c707 to your computer and use it in GitHub Desktop.
Group and order array by frequency
# Private: order companions by frequency.
#
# orders - Array of hashes.
#
# Returns Hashmap.
def companions(orders)
orders.reduce([]) {|acc, o| acc + o[:unique_product_ids]}.group_by(&:itself).transform_values!(&:size)
.sort{ |a,b| b.second <=> a.second }.first(15)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment