In which structure would it be more expedient to store a large (up to 6 million values) list of natural numbers so that you can quickly find that one random natural number falls into this list?

I tried to store everything as strings and use the sorted TstringList without repetitions:

 a.Sorted:=True; a.Duplicates:=dupIgnore; 

and in it already to check hit through Find :

 a.Find(MyNumber); 

but I think this is not the best way.

  • 2
    What version of Delphi? If 2010 and newer - you can use TDictionary <integer (Double), Boolean> - the search will be very fast. - kami
  • @ anton-shchyrov, kami I use Delphi XE5. Regarding the dictionaries, I read that it may not be very good in the case of a large number of elements: Habr , or in my case (6 million) can be neglected? Now I will try with a DB and TList. - Nikolay.OAMP
  • one
    Try it. It will take 1 minute to make a cycle to add 6 million numbers to the dictionary. In TList, in contrast to TDictionary, the search speed is much worse. When using the database - do not forget about indexing. - kami

1 answer 1

Options:

  1. DB. For 6 million records the most
  2. Sorted TList<Cardinal> or TDictionary<Cardinal, ...> if you only want to check for equality
  3. If Delphi is old, then the usual sorted TList with coercion of numbers to Pointer