www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why uniq do not work with array of strings?

reply "Suliman" <evermind live.ru> writes:
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.
Feb 16 2015
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
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.
Feb 16 2015
next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
Docs will get a lot better in the next release:

http://dlang.org/phobos-prerelease/std_algorithm_iteration.html#uniq
Feb 16 2015
prev sibling parent reply "Suliman" <evermind live.ru> writes:
 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...
Feb 16 2015
parent reply "Suliman" <evermind live.ru> writes:
Oh I understood. It's means that it work only of two or more 
element's is placed one after one?
Feb 16 2015
next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
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.
Feb 16 2015
prev sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
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.
Feb 16 2015