uniq and array of enum members (That are all strings)

bauss jj_1337 at live.dk
Wed Jan 16 16:37:21 UTC 2019


On Wednesday, 16 January 2019 at 16:35:04 UTC, H. S. Teoh wrote:
> On Wed, Jan 16, 2019 at 04:21:12PM +0000, bauss via 
> Digitalmars-d-learn wrote:
>> On Wednesday, 16 January 2019 at 16:12:28 UTC, H. S. Teoh 
>> wrote:
> [...]
>> > .uniq only works on adjacent identical elements.  You should 
>> > sort your array first.
>> > 
>> > If you need to preserve the original order but eliminate 
>> > duplicates, then you could use an AA to keep track of what 
>> > has been seen. E.g.:
>> > 
>> > 	bool[string] seen;
>> > 	auto b = a.filter!((e) {
>> > 			if (e in seen) return false;
>> > 			seen[e] = true;
>> > 			return true;
>> > 		});
> [...]
>> Sorting will not work in my case though because it's an enum 
>> of strings that are not sorted alphabetically.
>> 
>> Right now I'm doing it manually by a foreach in similar way 
>> you're using filter.
>> 
>> I just feel like that's an overkill for something so trivial.
>
> It's not trivial. In order for the computer to know whether or 
> not the i'th element should be excluded, it needs to know what 
> has come before it.

That's not necessarily true.

It just has to know whether the element matches an earlier 
element.

Your filter example demonstrates exactly why sorting isn't 
necessary.




More information about the Digitalmars-d-learn mailing list