Is there a function for this?
bauss
jj_1337 at live.dk
Sat Oct 6 13:55:32 UTC 2018
On Saturday, 6 October 2018 at 13:34:44 UTC, Adam D. Ruppe wrote:
> On Saturday, 6 October 2018 at 13:17:22 UTC, bauss wrote:
>> My first thought was "uniq", but it can't really do it like
>> that, but it doesn't work.
>
> uniq needs it to be sorted first, it only compares side-by-side
> (to avoid allocating space to remember what it has already seen)
>
>> Is there another function in Phobos that perhaps would work?
>
> If the set is small enough and it is at least a forward range,
> you could do a O(n^2) search with like filter with !canFind
> (making sure the one it finds is not the one you are currently
> looking at).
>
> But I think you are better off doing your own thing. I like to
> just stick them all in an associative array, then loop over
> that. The duplicate keys will naturally be filtered that way.
>
> Of course, it allocates memory for that but that's often
> preferable to running the expensive search anyway.
It "can" be a big set, but it's probably the most straightforward
way for now thought.
On Saturday, 6 October 2018 at 13:38:34 UTC, Nicholas Wilson
wrote:
> On Saturday, 6 October 2018 at 13:17:22 UTC, bauss wrote:
>> Let's say you have a range with struct, but some of the struct
>> are duplicates of each other.
>>
>> Is there a standard function in Phobos to remove duplicates?
>>
>> My first thought was "uniq", but it can't really do it like
>> that, but it doesn't work.
>>
>> See: https://run.dlang.io/is/IcFEtw
>>
>> Is there another function in Phobos that perhaps would work?
>>
>> I can of course write my own function, but if there is a
>> standard function that can do it, then I'd rather use that.
>
> range
> .array.sort // make equal elements be adjacent
> .chunks!"a==b" // get a range over those equal elements
> .map(e => e.front); // get the first one
>
> should work, possibly not the most efficient way to do it
> though.
>
> I don't know off the top of mu head if there are any to
> preserve the order or the original range.
That doesn't work tbh. won't even compile.
More information about the Digitalmars-d-learn
mailing list