I have a table Rating
- row1 = (V1, K1, 5);
- row2 = (V2, K2, 7);
- row2 = (V1, K1, 3);
I need to aggregate the Points in an AggregateRating table
such that the table contains
- row1 = (V1, K1, 8);
- row2 = (V2, K2, 7);
I loop once through the Rating table and
create a map with [key = (Col1,Col2), value = Points]
If the key exists I add the points else create a new map entry.
The Rating table can contain close to 100+ entries so I wanted to avoid making multiple passes.
Is this the most efficient way to go about it??
It depends on what data structure you use to store the map. If you use hash map than storing and reading from it will be constant in complexity (
O(1)). Then you make a single pass with one query and at most one insert for every entry in the array. This means that the whole complexity of your algorithm will beO(n).However, the input alone is
O(n), which means you can not do any better.Keep in mind that if you choose different implementation of the map, e.g. tree map, the complexity will change and you will not get the most efficient solution.