How to sum each unique element of an array within an array
ruby
Solution
a.group_by(&:first).map { |k,v| [k,v.map(&:last).inject(:+),v.size] }
# => [[1, 84, 1], [11, 113, 2], [12, 18, 1], [15, 59, 2]]
Problem
I have an array like: ``` [1, 84] [11, 29] [11, 84] [12, 18] [15, 55] [15, 4] ``` I Want to generate a new array from this array like: ``` [1, 84, 1] [11, 113, 2] [12, 18, 1] [15, 59, 2] ``` where the first element of the each sub-array will be unique and their corresponding second element will be sum, and the third element will be their count. In case of `11`, `29 + 84 = 113`, and `11` exists `2` times, so `[11, 113, 2]`.