DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

RemoveDuplicateStrings

Removes duplicate strings from the given string list. The modified string list will be sorted.

procedure RemoveDuplicateStrings(const Strings: Classes.TStrings);
var
  TempStrings: Classes.TStringList;
  Cnt: Integer;
begin
  if Strings.Count <= 1 then
    Exit;
  TempStrings := Classes.TStringList.Create;
  try
    TempStrings.Sorted := True;
    TempStrings.Duplicates := Classes.dupIgnore;
    for Cnt := 0 to Strings.Count - 1 do
      TempStrings.Add(Strings[Cnt]);
    Strings.Assign(TempStrings);
  finally
    TempStrings.Free;
  end;
end;

StripDuplicateStrings

Returns a string array that is a copy of given array SA with duplicates removed. The returned array will be sorted.

function StripDuplicateStrings(const SA: array of string):
  Types.TStringDynArray;
var
  SL: Classes.TStringList;
begin
  SL := Classes.TStringList.Create;
  try
    ArrayToStringList(SA, SL);
    RemoveDuplicateStrings(SL);
    Result := StringListToArray(SL);
  finally
    SL.Free;
  end;
end;

View the whole database.

Go to the DelphiDabbler website.