I need a suggestion on how to merge possibly more than 2 lists based on 2 key id values. I have a list of MyType where MyType contains id1,id2,value fields. I would get a random number of these lists(lets say n in number) and I am expected to merge them and form a new list. The new list should be like this List of Id1,id2,value[n] ... If id1,id2 matches in the remaining lists they should be added to value[] otherwise a null value would be added.
For ex:
List1 :- new list ({id1,id2,”Val1”},{id3,id4,”Val2”});
List2 :- new list ({id1,id2,”Val3”},{id3,id5,”Val4”}, {id6,id4,”Val5”});
List3 :- new list ({id1,id2,”Val4”},{id3,id4,”Val7”},{id6,id7,”Val8”},{id6,id4,”Val9”});
After merging the new list should contain:
MergeIist:- {id1,id2,[“Val1”,”val3”,”val4”]},{id3,id4,[“Val2”,NULL,”Val7”]},{id3,id5,[NULL,”Val4”,NULL]},
{ id6,id4,[NULL,”Val5”,”Val9”] },{ id6,id7,[NULL,NULL,”Val8”] }
I have an algorithm in mind but that’s very expensive like adding a flag to each entry in the list like “already processed” and comparing all the entries in the list and merging. Is there a smart way to handle this problem?