Search code examples
c#aggregatecalculatoraggregationmathematical-optimization

How to aggregate data integer in most efficient way?


I need to aggregate some array values from APIs in a background processing job in c#. The number of APIs can in theory be infinite but will probably never be more than 10.

The APIs return a simple list with UserID and Value. To use the data in my application I need to sum all values for each distinct UserID.

Example-data from two APIs:

From API 1:

[1, 240] (Note: The data is organized as [UserID, Value])
[2, 160]
[3, 12568]
[4, 1780]
[...]

From API 2:

[1, 10]
[2, 10]
[3, 10]
[4, 10]
[...]

Desired result:

[1, 250]
[2, 170]
[3, 12578]
[4, 1790]
[...]

How do I, in the most efficient way make this one desired list where the values for each UserID has been added to each other?

I've been looking at Matrixes which seems to be a way but I can't figure out how to do add values without also adding the UserIDs to each other?

(I realize it can be solved with some nested loops but I'm looking for a better way);

Note: The UserID's are not sequential.


Solution

  • Using Dictionary<int,int> will probably be faster than anything you can cook up in any other way, even with lookup of whether UserId is already contained as key.

    ie:

    var results = Dictionary<int,int>();
    foreach (var api in apis)
        foreach(var value in api.GetValues())
            if (!result.HasKey(value.UserID))
                result[value.UserId] = value.Value;
            else
                result[value.UserId] += value.Value;