Thread overview
Why uniq do not work with array of strings?
Feb 16, 2015
Suliman
Feb 16, 2015
Tobias Pankrath
Feb 16, 2015
Tobias Pankrath
Feb 16, 2015
Suliman
Feb 16, 2015
Suliman
Feb 16, 2015
Tobias Pankrath
Feb 16, 2015
Tobias Pankrath
February 16, 2015
The question appear here
http://stackoverflow.com/questions/28546572/how-to-find-duplicates-in-array-of-strings-in-d

I can't understand, why uniq work for array of int but do not work with array of strings.

	int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
	writeln(uniq(arr));

	string [] str = ["qwe","asd","zxc", "qwe"];
	auto uniqarr = uniq(str);

	foreach(x;uniqarr)
	{
		writeln(x);
	}

Running .\test.exe
[1, 2, 3, 4, 5]
qwe
asd
zxc
qwe

^ "qwe" prints two times.
February 16, 2015
On Monday, 16 February 2015 at 18:28:13 UTC, Suliman wrote:
> The question appear here
> http://stackoverflow.com/questions/28546572/how-to-find-duplicates-in-array-of-strings-in-d
>
> I can't understand, why uniq work for array of int but do not work with array of strings.
>
> 	int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
> 	writeln(uniq(arr));
>
> 	string [] str = ["qwe","asd","zxc", "qwe"];
> 	auto uniqarr = uniq(str);
>
> 	foreach(x;uniqarr)
> 	{
> 		writeln(x);
> 	}
>
> Running .\test.exe
> [1, 2, 3, 4, 5]
> qwe
> asd
> zxc
> qwe
>
> ^ "qwe" prints two times.

Works as expected. From the docs:

> Iterates unique -> _consecutive_ <- elements of the given range

Though I concur that the description might be more verbose.
February 16, 2015
Docs will get a lot better in the next release:

http://dlang.org/phobos-prerelease/std_algorithm_iteration.html#uniq
February 16, 2015
> Iterates unique -> _consecutive_ <- elements of the given range
Could you explain what does it's mean? I do not understand what is _consecutive_ mean in this content... and why it's not work with strings...
February 16, 2015
Oh I understood. It's means that it work only of two or more element's is placed one after one?
February 16, 2015
On Monday, 16 February 2015 at 18:45:17 UTC, Suliman wrote:
> Oh I understood. It's means that it work only of two or more element's is placed one after one?

Yes, uniq returns exactly the same range as its input, except that elemens that are equal to their immediate predecessor are dropped.
February 16, 2015
On Monday, 16 February 2015 at 18:45:17 UTC, Suliman wrote:
> Oh I understood. It's means that it work only of two or more element's is placed one after one?

That's why you'll usually want to sort before using uniq.