Fastest way to delete a TStrings item without sort

159 Views Asked by At

I plan to develop software that does not use the sorted property with TStrings class and remove duplicate items. What is the best way to do this performance wise? Thanks for your expertise as always or direction to follow. Hope I am clear with the question, basically a list of text where duplicate lines are removed yet remain unsorted. The file pertains to IRC info and it can be somewhat large in size. I think the log last I checked was around 1.2 megs or so.

TIA for your words of wisdom

1

There are 1 best solutions below

10
MBo On

It is better to avoid duplicates, so keep dictionary (here it is used as set) with strings already in the list.

Example:

uses System.Generics.Collections;

var
  MyDict: TDictionary<String, Integer>;
  s: string;
  sl: TStringList;
  strs: array of string;
begin
  strs := ['aa', 'bb', 'aa', 'dd'];
  sl := TStringList.Create;
  try
    MyDict := TDictionary<String, Integer>.Create;
    try
      for s in strs do
         if Mydict.TryAdd(s, 1) then
            sl.Add(s);
      Memo1.Lines.Assign(sl);
    finally
      MyDict.Free;
    end;
  finally
    sl.Free;
  end;