Sorting is in O(n log n) and requires a random access range (=allocation!)
However plain iteration can be done in O(n) and groups can be created with a simple hashmap and e.g. the simplest implementation of uniq is sth. like this:
auto uniq(R)(R r)
{
import std.algorithm.iteration : each;
bool[dchar] d;
r.each!(key => d[key] = true);
return d.byKey;
}
unittest
{
import std.algorithm.iteration : walkLength;
assert("cbbba"d.uniq.walkLength == 3);
}
The idea is that for the common use case (unsorted ranges), these non-lazy range functions allocate less and should perform faster.
Comment #1 by robert.schadek — 2024-12-01T16:28:26Z