Function which returns a sorted array without duplicates

Ali Çehreli acehreli at yahoo.com
Sun Jan 22 19:07:44 UTC 2023


On 1/21/23 23:33, evilrat wrote:

 > And IIRC you probably don't need `dup`

Unfortunately, no. Skipping .dup is only possible if we are allowed to 
sort the original array.

 > as sort produces a lazy range.

sort() returns a SortedRange but it can't be lazy. Even if it were, the 
first call to .front would have to sort anyway. However...

There is an optimization possible for such requirements if not all 
elements but just a number of them are needed. For example, if only the 
first 10 elements are needed, then a binary heap may be faster:

   https://dlang.org/phobos/std_container_binaryheap.html

The usage would be similar to the following for "the first 10 unique 
elements":

   heapify(arr).uniq.take(10)

(Not tested.)

Ali



More information about the Digitalmars-d-learn mailing list