How to remove a record if it is duplicate end sum values :val

saw something similar somewhere as a test for job seekers :slight_smile:

so don't know if someone will help you as it's not so hard to solve

tom

Michael Pavling, thanks for reply.

Okay, i agree that is a array of hash : list.class => Array end list.last.class => Hash. But your solution only removes duplication and the sum of :val ?

Tom Meinlschmid, thanks for reply.

Not a test for job seekers :slight_smile:

Solution:

list.inject(Hash.new(0)) { |hash, el| hash[el[:cod]] += el[:val] ; hash }

Michael Pavling, thanks for reply.

Okay, i agree that is a array of hash : list.class => Array end list.last.class => Hash. But your solution only removes duplication and the sum of :val ?

I think you need to make it more clear what you are trying to achieve. The sentence " But your solution only removes duplication and the sum of :val?" does not make sense.

Colin

Iā€™m sorry if it made no sense to my sentence. The next will be clearer.