AW: Doppelte Strings in einer Liste finden
26. Jan 2017, 19:53
Wie wäre es hiermit?
Delphi-Quellcode:
procedure TForm1.Button4Click(Sender: TObject);
type
PHisto = ^THisto;
THisto = packed record
sString: string;
iCount: Integer;
end;
function Compare(Item1, Item2: THisto): Integer;
begin
Result := THisto(Item2).iCount - THisto(Item1).iCount;
end;
var
Histo: PHisto;
HistoList: TList;
i, j: Integer;
bCont: Boolean;
begin
Memo2.Lines.Clear;
HistoList := TList.Create;
try
for i := 0 to Memo1.Lines.Count - 1 do
begin
bCont := True;
New(Histo);
Histo^.iCount := 0;
Histo^.sString := Memo1.Lines.Strings[i];
for j := 0 to HistoList.Count - 1 do
begin
if PHisto(HistoList.Items[j]).sString = Histo^.sString then
begin
bCont := False;
Dispose(PHisto(Histo));
Break;
end;
end;
if bCont then
begin
for j := 0 to Memo1.Lines.Count - 1 do
begin
if Memo1.Lines.Strings[j] = Histo^.sString then
Inc(Histo^.iCount);
end;
HistoList.Add(Histo);
end;
end;
HistoList.Sort(@Compare);
for i := 0 to HistoList.Count - 1 do
Memo2.Lines.Add(IntToStr(PHisto(HistoList.Items[i]).iCount) + ' - ' + PHisto(HistoList.Items[i]).sString);
finally
for i := HistoList.Count - 1 downto 0 do
begin
Dispose(PHisto(HistoList.Items[i]));
HistoList.Delete(i);
end;
HistoList.Free;
end;
end;
Geändert von a.def (26. Jan 2017 um 20:04 Uhr)
|