AW: Delete duplicates from list
^^ Ich habe das jetzt nochmal ins Reine geschrieben und versucht, dabei möglichst simpel zu bleiben.
Delphi-Quellcode:
Wer mag, kann auch auf StrUtils u.a. zurückgreifen, so sollte es aber schon funktionieren.
procedure DeleteDuplicates(const List: TStrings);
var CurrentIndex, FollowingIndex: integer; begin Assert(Assigned(List)); List.BeginUpdate; try CurrentIndex := 0; while CurrentIndex < List.Count - 1 do begin for FollowingIndex := List.Count - 1 downto CurrentIndex + 1 do if List[CurrentIndex] = List[FollowingIndex] then List.Delete(FollowingIndex); Inc(CurrentIndex); end; finally List.EndUpdate; end; end; procedure DeleteDuplicatesCaseInsensitive(const List: TStrings); var CurrentIndex, FollowingIndex: integer; begin Assert(Assigned(List)); List.BeginUpdate; try CurrentIndex := 0; while CurrentIndex < List.Count - 1 do begin for FollowingIndex := List.Count - 1 downto CurrentIndex + 1 do if AnsiLowerCase(List[CurrentIndex]) = AnsiLowerCase(List[FollowingIndex]) then List.Delete(FollowingIndex); Inc(CurrentIndex); end; finally List.EndUpdate; end; end; |
AW: Delete duplicates from list
Die äussere For-Schleife hat aber mehr Durchläufe als nötig.
Ich würde da eine While-Schleife verwenden, die dann abbricht wenn es nichts mehr zu tun gibt. Ungetestet:
Delphi-Quellcode:
Hätte die Stringliste z.B. 1000 identische Einträge würde die While-Schleife schon nach einem Durchlauf abbrechen, während eine For-Schleife alle restlichen 999 Einträge erfolglos durchtesten würde.
CurrentIndex := 0
while CurrentIndex < List.Count - 1 do begin for FollowingIndex := List.Count - 1 downto CurrentIndex + 1 do if List[CurrentIndex] = List[FollowingIndex] then List.Delete(FollowingIndex); Inc(CurrentIndex); end; |
AW: Delete duplicates from list
Stimmt, ich hatte vergessen, dass for die Anzahl nur einmalig ermittelt.
[edit] Korrigiert [/edit] |
AW: Delete duplicates from list
Das selbst in soetwas scheinbar banalem der Teufel (nun ja, ein Teufelchen) im Detail stecken kann...
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:54 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz