# What is the difference between these codes?

**URL:** <https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060>\
**Category:** Questions\
**Created:** [May 20, 2022, 10:42pm UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060 "2022-05-20T22:42:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amerabouassaleh](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@amerabouassaleh](https://community.wolfram.com/u/amerabouassaleh)\
**Post date:** [May 20, 2022, 10:42pm UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060/1 "2022-05-20T22:42:21Z")

</div>

Union [StringJoin /@ Permutations [ Characters [ “hello”]]]

```
StringJoin /@ Permutations [Characters [ "hello"]]

```

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [May 20, 2022, 11:16pm UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060/2 "2022-05-20T23:16:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [May 20, 2022, 11:22pm UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060/3 "2022-05-20T23:22:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![amerabouassaleh](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@amerabouassaleh](https://community.wolfram.com/u/amerabouassaleh)\
**Post date:** [May 21, 2022, 12:23am UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060/4 "2022-05-21T00:23:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [May 21, 2022, 2:36pm UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060/5 "2022-05-21T14:36:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [May 22, 2022, 4:04pm UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060/6 "2022-05-22T16:04:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kmosby1992](https://avatars.discourse-cdn.com/v4/letter/k/a183cd/32.png) [@kmosby1992](https://community.wolfram.com/u/kmosby1992)\
**Post date:** [May 23, 2022, 9:21pm UTC](https://community.wolfram.com/t/what-is-the-difference-between-these-codes/20060/7 "2022-05-23T21:21:57Z")

</div>

Have you searched the [doc](https://www.wolframalpha.com/)?
