Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I quickly remove duplicates from a list box?

I wish to remove duplicate items from a large TListBox. To do that I use a classic simple method. It works, but it takes 19 minutes. I read a lot and apparently I should use a TFileStream (?). But I don't 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 the speed?

like image 810
Beny Avatar asked Jul 15 '11 00:07

Beny


People also ask

What is the Excel formula to remove duplicates?

To begin with, select the range in which you want to ddelete dupes. To select the entire table, press Ctrl + A. Go to the Data tab > Data Tools group, and click the Remove Duplicates button. The Remove Duplicates dialog box will open, you select the columns to check for duplicates, and click OK.


1 Answers

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;
like image 79
Daniel Maurić Avatar answered Sep 30 '22 10:09

Daniel Maurić