Message Boards Message Boards

0
|
1897 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

What is the difference between these codes?

Posted 2 years ago
Union [ StringJoin /@ Permutations [ Characters [ "hello" ]]]

StringJoin /@ Permutations [ Characters [ "hello" ]]
POSTED BY: Amer Assaleh
6 Replies
Posted 2 years ago

Have you searched the doc?

POSTED BY: Kevin Mosby
Posted 2 years ago

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?

POSTED BY: Amer Assaleh

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.

POSTED BY: Daniel Lichtblau
Posted 2 years ago

You can use DuplicateFreeQ to test if a List has duplicates.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

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.

POSTED BY: Rohit Namjoshi

One is applying Union, the other is not. What is this question about? Is one or the other giving an unexpected result?

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract