Find duplicates in a stringlist very fast

delphi

Solution

Just for completeness, (and because your code doesn't actually use the duplicate, but just indicates one has been found): Delphi's `TStringList` has the built-in ability to deal with duplicate entries, in it's `Duplicates` property. Setting it to `dupIgnore` will simply discard any duplicates you attempt to add. Note that the destination list has to be sorted, or `Duplicates` has no effect.

TestStringList.Sorted := True;
TestStringList.Duplicates := dupIgnore;

for i := 0 to  DataStringList.Items-1 do
   TestStringList.Add(DataStringList[i]);
Memo1.Lines.Add(Format('%d duplicates discarded',
                      [DataStringList.Count - TestStringList.Count]));

A quick test shows that the entire loop can be removed if you use `Sorted` and `Duplicates`:

TestStringList.Sorted := True;
TestStringList.Duplicates := dupIgnore;

TestStringList.AddStrings(DataStringList);
Memo1.Lines.Add(Format('%d duplicates discarded',
                      [DataStringList.Count - TestStringList.Count]));

See the `TStringList.Duplicates` documentation for more info.

Problem

what is the fastest way to find duplicates in a Tstringlist. I get the data I need to search for duplicates in a Stringlist. My current idea goes like this : ``` var TestStringList, DataStringList : TstringList; for i := 0 to DataStringList.Items-1 do begin if TestStringList.Indexof(DataStringList[i])< 0 < 0 then begin TestStringList.Add(DataStringList[i]) end else begin memo1.ines.add('duplicate item found'); end; end; .... ```

Original source

Related problems