You have the wrong data structure since you really need a dictionary .
The main problem with using a list is that you want to search on a subset of a saved record. But lists are not configured for this. Solve the problem by re-writing with TDictionary<Integer, Integer> .
I can recommend you carefully read the dictionary example in the Embarcadero docwiki docs .
The key to the dictionary is what you call comb , and the value is freq . To add an item, you do this:
if Dict.TryGetValue(Comb, Freq) then Dict[Comb] := Freq+1 else Dict.Add(Comb, 1);
I assume your dictionary is declared as follows:
var Dict: TDictionary<Integer, Integer>;
and is created as follows:
Dict := TDictionary<Integer, Integer>;
You can list the dictionary with a simple for in loop.
var Item: TPair<Integer, Integer>; ... for Item in Dict do Writeln(Item.Key:3, Item.Value:10);
Although it should be warned that the dictionary will be listed in odd order. You can sort before printing.
If you want to store additional information associated with each entry in the dictionary, put additional fields in the entry.
type TDictValue = record Freq: Integer; Field1: string; Field2: TDateTime;
Then your dictionary will become TDictionary<Integer, TDictValue> .
David heffernan
source share