how to get top N distinct elements from range?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Mar 9 12:01:45 PST 2013


On 3/8/13 1:34 PM, Chris Cain wrote:
> On Friday, 8 March 2013 at 18:17:22 UTC, bearophile wrote:
>> auto firstDistinct(Range)(Range r, in size_t n) {
>> bool[ForeachType!Range] mySet;
>>
>> return r.filter!((k) {
>> if (k in mySet)
>> return false;
>> mySet[k] = true;
>> return true;
>> }).take(n);
>> }
>>
>
> I have to say, that's a great example of beautiful simplicity in
> programming. Bravo.

Yah, impressive. Nice work bearophile.

Andrei




More information about the Digitalmars-d-learn mailing list