# Creating a unique list of elements

**URL:** <https://swi-prolog.discourse.group/t/creating-a-unique-list-of-elements/7101>\
**Category:** Help!\
**Created:** [December 22, 2023, 4:33pm UTC](https://swi-prolog.discourse.group/t/creating-a-unique-list-of-elements/7101 "2023-12-22T16:33:13Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![fork](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/fork/32/6050_2.png) [@fork](https://swi-prolog.discourse.group/u/fork)\
**Post date:** [December 22, 2023, 4:33pm UTC](https://swi-prolog.discourse.group/t/creating-a-unique-list-of-elements/7101/1 "2023-12-22T16:33:13Z")

</div>

Hi all,

I’m a bloody prolog-Newbie.

I’m using: SWI-Prolog version 9.04

I want the code to:

Deliver a list of unique combinations.

```prolog
$ swipl test.pl

?- tst(A, B).

A = berlin
B = frankfurt ;
A = frankfurt,
B = berlin ;

```

But what I’m getting is:

A list with duplicates.

```prolog
$ swipl test.pl

?- tst(A, B).

A = berlin,
B = frankfurt ;
A = berlin,
B = frankfurt ;
A = frankfurt,
B = berlin ;
A = frankfurt,
B = berlin ;
false.

```

My code looks like this:

```prolog
city(berlin).
city(frankfurt).

unique([]).
unique([_]).
unique([H|T]) :-
    \+ contains(T, H),
    unique(T).

contains([], _) :- false.
contains([H|_], H).
contains([_|T], X) :- contains(T, X).

tst(A, B) :-
        city(A),
        city(B),
        unique([A,B]).

```

Thanks for any hints to learn, what I missed or failed to understand or explanations what is really going on here!

---

_[View the full topic](https://swi-prolog.discourse.group/t/creating-a-unique-list-of-elements/7101)._
