Ok, wie wäre es so (Zum Testen eine ListBox und ein Button):
Delphi-Quellcode:
procedure TForm149.Button1Click(Sender: TObject);
procedure CountOccurrences(const MyList: TStrings; var Result: TStrings);
var
i, CurIndex: Integer;
begin
for i := 0 to MyList.Count - 1 do
begin
CurIndex := Result.IndexOf(MyList[i]);
if CurIndex >= 0 then
Result.Objects[CurIndex] := TObject(Succ(Integer(Result.Objects[CurIndex])))
else
Result.AddObject(MyList[i], TObject(1));
end;
end;
var
TempList: TStrings;
i: Integer;
begin
TempList := TStringList.Create;
try
CountOccurrences(ListBox1.Items, TempList);
for i := 0 to TempList.Count - 1 do
ShowMessage(TempList[i] + ': ' + IntToStr(Integer(TempList.Objects[i])));
finally
TempList.Free;
end;
end;