출처 : http://delphi.about.com/od/delphitips2009/qt/remove-duplicat.htm
TMemo / TComboBox / TListBox등에서 모두 사용가능한 방법이다.
procedure RemoveDuplicates(const stringList : TStringList) ; Var Buffer : TStringList; Cnt : Integer; begin StringList.Sort; Buffer := TStringList.Create; Try Buffer.Sorted := True; Buffer.Duplicates := dupIgnore; Buffer.BeginUpdate; For Cnt := 0 To stringList.Count - 1 Do begin Buffer.Add(StringList[cnt]) ; end; Buffer.EndUpdate; StringList.Assign(Buffer) ; Finally FreeandNil(Buffer) ; End; end;
[사용 예]
var sl : TStringList; cnt : integer; begin Randomize; sl := TStringList.Create; Try For cnt := 1 To 100 Do sl.Add(IntToStr(Random(100))) ; sl.Sorted := True; sl.Duplicates := dupIgnore; ShowMessage('With duplicates: ' + #13#10 + IntToStr(sl.Count)) ; RemoveDuplicates(sl) ; ShowMessage('Without duplicates: ' + #13#10 + IntToStr(sl.Count)) ; Finally sl.Free; End;