List filtering

I have a ListBox with 10,000 items and many many duplicate items! I want to save it to a file without duplicate elements (one item instead of all copies!) And I use this method:

Function TMain.List_ExistsIn(ListBox_NAme: TListBox; EParameter: String): Integer;
Var
  i: Integer;
Begin
  EParameter := LowerCase(EParameter);
  Result := -1;
  For i:=0 To ListBox_Name.Items.Count - 1 Do
    If EParameter = Lowercase(ListBox_Name.Items[i]) Then Begin
      Result := i;
      Break;
    End;
End;

I use the code above to find an existing element and the following procedure to save it:

Procedure TMain.MakeList(ListBox_Name: TListBox; FileName: String); //================
Var
  i: Integer;
  Temp_ListBox: TListBox;
Begin
  Temp_ListBox := TListBox.Create(Main);
  With Temp_ListBox Do Begin
    Parent := Main;
    Clear;
    For i:=0 To ListBox_Name.Count - 1 Do
      If Main.List_ExistsIn(Temp_ListBox, ListBox_Name.Items[i]) = -1 Then
        Items.Add(ListBox_Name.Items[i]);
    Items.SaveToFile(FileName);
    Free;
  End;
End;

But this is a very long time. Is there a better and faster way? Thank.

+3
source share
2 answers

Try this one

procedure TForm1.FormCreate(Sender: TObject);
var
  StrList: TStringList;
  I: Integer;
begin
  StrList := TStringList.Create;
  StrList.Sorted := True;
  StrList.Duplicates := dupIgnore;
  StrList.AddStrings(ListBox1.Items);  //Your List Box Items
  StrList.SaveToFile('C:\abc.txt');
  StrList.Free; //Cleanup
end;
+7
source

Watch out for the shameful effect of CompareString () ...

Insert 59A, 5-9A, 59-A, -59-A into the sorted list 1. The list becomes 59A, -59-A, 5-9A, 59-A and .Find () or .IndexOf () cannot find 59 -A.

2, 59A, -59-A, 5-9A, 59-A. 59A, 59-A, -59-A, 5-9A. .Find() .IndexOf() 59-A.

. .

+1

Source: https://habr.com/ru/post/1750982/


All Articles