# How to avoid duplicate return values

**URL:** <https://swi-prolog.discourse.group/t/how-to-avoid-duplicate-return-values/1508>\
**Category:** Help!\
**Created:** [November 15, 2019, 12:43pm UTC](https://swi-prolog.discourse.group/t/how-to-avoid-duplicate-return-values/1508 "2019-11-15T12:43:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rflook](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@rflook](https://swi-prolog.discourse.group/u/rflook)\
**Post date:** [November 15, 2019, 12:43pm UTC](https://swi-prolog.discourse.group/t/how-to-avoid-duplicate-return-values/1508/1 "2019-11-15T12:43:46Z")

</div>

I’m using: SWI-Prolog version 7.2.3

I want the code to: Return values which are not duplicates

But what I’m getting is: Duplicate values

My code looks like this:

```prolog
parent(patch,spot).
parent(patch, fluffy).
parent(fang,spot).
parent(patch,tommy).
parent(fang,tommy).
parent(kitty,ranger).
parent(fairy,fang).
parent(frodo,fang).
parent(frodo,kitty).

/* CREATE A RULE FOR FINDING SIBLINGS - this returns the same value twice if the child cat has two shared parents */
sibling(A,B):-
	parent(C,A),
	parent(C,B),
	not(A=B).

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 15, 2019, 12:45pm UTC](https://swi-prolog.discourse.group/t/how-to-avoid-duplicate-return-values/1508/2 "2019-11-15T12:45:39Z")

</div>

> [@rflook](#):
>
> Return values which are not duplicates

See this StackOverflow answer: [Select unique elements in Prolog](https://stackoverflow.com/questions/50190457/select-unique-elements-in-prolog)

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [November 15, 2019, 1:09pm UTC](https://swi-prolog.discourse.group/t/how-to-avoid-duplicate-return-values/1508/3 "2019-11-15T13:09:15Z")

</div>

@EricGT Using `setof/3` to hide identical solutions is a bad idea in the general case. You should avoid generating them, if possible, instead of generating them, then aggregating them, then throwing them away.

In this case, `setof/3` doesn’t help anyway: the solutions are _equivalent_ but they are not _equal_, so sorting does nothing.

In this particular case, you could replace `not(A=B)` with `A @< B`, for example.

@rflook You are using an ancient version of SWI-Prolog 🙂

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 15, 2019, 1:57pm UTC](https://swi-prolog.discourse.group/t/how-to-avoid-duplicate-return-values/1508/4 "2019-11-15T13:57:38Z")

</div>

There are roughly two options. One is improve your logic to avoid double solutions. If you consider  
(A.B) and (B,A) as the same, use `A @> B` instead of `not(A=B)` (which you better write as  
`\+ A = B` or even better as `A \= B`). The other is to use SWI-Prolog’s [distinct/1,2](https://www.swi-prolog.org/pldoc/doc_for?object=distinct/1). This is SWI-Prolog specific. setof/3 and member also do the job, but compute all results before returning the first. Distinct/1,2 requires a newer version though.
