How to quickly remove duplicates from a list?

I want to remove duplicate elements from a large TListBox. For this, I use the classic simple method. It works, but it takes 19 minutes. I read a lot and apparently should use TFileStream (?). But I dont know how.

My classic method is this:

procedure NoDup(AListBox : TListBox); var i : integer; begin with AListBox do for i := Items.Count - 1 downto 0 do begin if Items.IndexOf(Items[i]) < i then Items.Delete(i); Application.ProcessMessages; end; end; 

How can I improve speed?

+4
source share
1 answer
 procedure NoDup(AListBox: TListBox); var lStringList: TStringList; begin lStringList := TStringList.Create; try lStringList.Duplicates := dupIgnore; lStringList.Sorted := true; lStringList.Assign(AListBox.Items); AListBox.Items.Assign(lStringList); finally lStringList.free end; end; 
+9
source

All Articles