Removing duplicate hashes in an array while keeping count of the times the hash was present I have an array of hashes (price_params['items']) where each item has a key called quantity what I'm trying to do is to clean every duplicate of each item but keeping the count of the times the item was found in the array in the unique one that I'm leaving (using the quantity key to keep that value)

This is my code so far:

def clean_duplicated_offers
  price_params['items'].each do |item|
    max_count = price_params['items'].count(item)
    next unless max_count &gt; 1
    price_params['items'].delete(item)
    item['quantity'] = max_count
    price_params['items'] &lt;&lt; item
  end
end


It works well, but it feels weird to delete every occurrence of the item in the array just to add it back to it.

Is there any better way to achieve this?
Does it have to be an Array? I.e. is the order important?

If not, then there is actually a data structure that does exactly what you want: the multiset. A multiset is just like a set, except that its elements have a multiplicity. In other words, a set can tell you whether or not an element is a member, a multiset in addition can tell you how often it is a member.

Basically, if you use a multiset, you will not have to do anything, since the multiset keeps track of the multiplicity (i.e. your quantity) for you.

There is no multiset in the Ruby core library or standard library, but there are a couple of third-party libraries and gems. I'll just grab one randomly, it doesn't really matter which one; their APIs are fairly similar.

require 'multiset'

price_params_items = %w[item2 item1 item3 item2 item3 item3]

result = Multiset[*price_params_items]
#=&gt; #&lt;Multiset:#2 "item2", #1 "item1", #3 "item3"&gt;


And that's it! You might ask yourself, where is the algorithm gone? That is a general property of programming: if you find the right data structure(s), the algorithm(s) become(s) much simpler, or in this case, even vanishes completely.

Unfortunately, for this specific implementation of multiset, there is no direct way to retrieve the multiplicity of an element, but you can convert it to a hash, and then you get what you need:

result.to_hash
#=&gt; { "item2" =&gt; 2, "item1" =&gt; 1, "item3" =&gt; 3 }