Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
February 16, 2015 Why uniq do not work with array of strings? | ||||
---|---|---|---|---|
| ||||
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 Re: Why uniq do not work with array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 Re: Why uniq do not work with array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | Docs will get a lot better in the next release: http://dlang.org/phobos-prerelease/std_algorithm_iteration.html#uniq |
February 16, 2015 Re: Why uniq do not work with array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | > 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 Re: Why uniq do not work with array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | Oh I understood. It's means that it work only of two or more element's is placed one after one? |
February 16, 2015 Re: Why uniq do not work with array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 Re: Why uniq do not work with array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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.
|
Copyright © 1999-2021 by the D Language Foundation