Union [ StringJoin /@ Permutations [ Characters [ “hello” ]]]
StringJoin /@ Permutations [ Characters [ "hello" ]]
Union [ StringJoin /@ Permutations [ Characters [ “hello” ]]]
StringJoin /@ Permutations [ Characters [ "hello" ]]
One is applying Union, the other is not. What is this question about? Is one or the other giving an unexpected result?
Perhaps you are wondering why the order is different? That is because the result from Union is always sorted. From the documentation
Union[list] gives a sorted version of a list, in which all duplicated
elements have been dropped.
If you want to delete duplicates without changing the order then use DeleteDuplicates. From the documentation
DeleteDuplicates never reorders elements, but only removes them.
Thank you,
Yes, now I can see that Union[] had sorted the list.
But (in this example) Permutation[] won’t produce duplications so Union[] is not been used properly here, right?
If you know in advance that your list has no duplicates then Union is not needed (and Sort can be used if you want it sorted). But in general one might not know a list will contain no duplicates.
You can use DuplicateFreeQ to test if a List has duplicates.
Have you searched the doc?