# How to delete the first occurence of EACH element of list?

**URL:** <https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [April 13, 2022, 3:44pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274 "2022-04-13T15:44:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwertyuiop](https://avatars.discourse-cdn.com/v4/letter/q/db5fbb/32.png) [@qwertyuiop](https://swi-prolog.discourse.group/u/qwertyuiop)\
**Post date:** [April 13, 2022, 3:44pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/1 "2022-04-13T15:44:43Z")

</div>

I know how to remove the first occurence of an element.  
For example, we have a list: [a,a,a,b,b,b,c,c,c,d,d,d].

```prolog
del(_, [], []).
del(X, [X|T], T).
del(X, [H|T], [H|R]) :- del(X, T, R).

?- del(a, [a,a,a,b,b,b,c,c,c,d,d,d], X)

```

From this code I will get the result [a,a,b,b,b,c,c,c,d,d,d].  
But how should I write the code to get [a,a,b,b,c,c,d,d] in result?

---

<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:** [April 13, 2022, 3:50pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/2 "2022-04-13T15:50:46Z")

</div>

Try

```prolog
?- L = [a,a,a,b,b,b,c,c,c,d,d,d],L=[Head|Tail].

```

When you try this it will show

```prolog
L = [a, a, a, b, b, b, c, c, c|...],
Head = a,
Tail = [a, a, b, b, b, c, c, c, d|...].

```

so you can do it this way

```prolog
?- L = [a,a,a,b,b,b,c,c,c,d,d,d],L=[Head|Tail];true.
L = [a, a, a, b, b, b, c, c, c|...],
Head = a,
Tail = [a, a, b, b, b, c, c, c, d|...] 

```

Then press the `w` key to show

```prolog
L = [a, a, a, b, b, b, c, c, c, d, d, d],
Head = a,
Tail = [a, a, b, b, b, c, c, c, d, d, d] .

```

See: [Help: I want the whole answer](https://www.swi-prolog.org/FAQ/AllOutput.html)

---

<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:** [April 13, 2022, 4:26pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/3 "2022-04-13T16:26:48Z")

</div>

What should happen if you have the list `[a, b, c]`? How about `[a, b, b, a, b, b]`? etc.

Maybe you should start by outlining the algorithm in detail.

---

<div class="post-metadata">

**Author:** ![qwertyuiop](https://avatars.discourse-cdn.com/v4/letter/q/db5fbb/32.png) [@qwertyuiop](https://swi-prolog.discourse.group/u/qwertyuiop)\
**Post date:** [April 13, 2022, 5:03pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/4 "2022-04-13T17:03:36Z")

</div>

If I have the list [a,b,c], I will get an empty list. If [**a** , **b** , b, a, b, b] → [b, a, b, b].

As an other example: [a, b, a, c, d, c, a, b, f, d, b, a, b].

- The very first element is “a”: [**a** , b, a, c, d, c, a, b, f, d, b, a, b]. We haven’t deleted any “a” element from this list. So, we should do it.

- Now our list is: [b, a, c, d, c, a, b, f, d, b, a, b] and the first element is “b”: [**b** , a, c, d, c, a, b, f, d, b, a, b]. We also haven’t deleted any “b” element - so, we need to remove “b”.

- Now our list is: [a, c, d, c, a, b, f, d, b, a, b]. The first element is “a”, but we’ve deleted an “a” element. So, we take “c” element and we haven’t deleted any “c” elements =\> we remove “c” from the list: [a, **c** , d, c, a, b, f, d, b, a, b] → [a, d, c, d, b, f, d, b, a, b].

- [a, **d** , c, d, b, f, d, b, a, b] → [a, c, d, b, f, d, b, a, b] : We haven’t deleted any “d” - we delete the first “d” in the list.

- [a, c, d, b, **f** , d, b, a, b] → [a, c, d, b, d, b, a, b] :We haven’t deleted any “f” - we delete the first “f” in the list.

- [a, c, d, b, d, b, a, b]: We’ve deleted “a”, “b”, “c”, “d” elements and we don’t have any other elements in the list

And the result is: [a, c, d, b, d, b, a, b]

---

<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:** [April 13, 2022, 8:13pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/5 "2022-04-13T20:13:59Z")

</div>

So you would need some book-keeping where you collect the elements you see for the first time as you traverse the list?

---

<div class="post-metadata">

**Author:** ![qwertyuiop](https://avatars.discourse-cdn.com/v4/letter/q/db5fbb/32.png) [@qwertyuiop](https://swi-prolog.discourse.group/u/qwertyuiop)\
**Post date:** [April 13, 2022, 8:50pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/6 "2022-04-13T20:50:18Z")

</div>

I think yes, because otherwise program will “think” each element as seen for the first time

---

<div class="post-metadata">

**Author:** ![qwertyuiop](https://avatars.discourse-cdn.com/v4/letter/q/db5fbb/32.png) [@qwertyuiop](https://swi-prolog.discourse.group/u/qwertyuiop)\
**Post date:** [April 15, 2022, 5:48pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/7 "2022-04-15T17:48:15Z")

</div>

Can anybody help me? I tried to do book-keeping, but I don’t know how.  
I also thought about other ways to resolve this problem. Maybe I need smth like this?

```prolog
del1(_,[],[]).
del1(El,[El|T],T1):-del1(El,T,T1).
del1(El,[X|T],[X|T1]):-not(El=X),del1(El,T,T1).
del([],L,L).
del([H|Tail],L,Ans):-del1(H,L,Temp),del(Tail,Temp,Ans).

```

```prolog
?- del([a,b,c,d,f], [a,b,a,c,d,c,a,b,f,d,b,a,b], X)

```

It removes elements that are in the first list(there I write which “types” of elements we have) from the second list. But it removes ALL elements and I don’t know how to change it to my purpose

---

<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:** [April 15, 2022, 5:58pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/8 "2022-04-15T17:58:00Z")

</div>

Is this homework? What are the limitations to what you can and cannot use? Can you use member/2? How about memberchk/2? How about \+/1?

---

<div class="post-metadata">

**Author:** ![qwertyuiop](https://avatars.discourse-cdn.com/v4/letter/q/db5fbb/32.png) [@qwertyuiop](https://swi-prolog.discourse.group/u/qwertyuiop)\
**Post date:** [April 15, 2022, 6:35pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/9 "2022-04-15T18:35:17Z")

</div>

Yes, this is homework, but about limitations is nothing said. I can use all this functions

---

<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:** [April 15, 2022, 7:29pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/10 "2022-04-15T19:29:00Z")

</div>

You have all the building blocks then. Maybe start by writing in pseudocode the algorithm. Something like:

```nohighlight
Input L: list,
Let Seen be empty list,
Let Result be empty list,
for each element E in L:
    if E not in Seen then... else...

```

and so on

---

<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:** [April 15, 2022, 8:49pm UTC](https://swi-prolog.discourse.group/t/how-to-delete-the-first-occurence-of-each-element-of-list/5274/11 "2022-04-15T20:49:34Z")

</div>

> [@qwertyuiop](#):
>
> we remove “c” from the list: [a, **c** , d, c, a, b, f, d, b, a, b] → [a, d, c, d, b, f, d, b, a, b].

You made a mistake here, you mutated an “a” into a “d”.
