Stay logically clean and efficient using if_/3 and (=)/3 @false. This happens as follows:
list_member1x([X|Xs],E) :- if_(X=E, maplist(dif(E),Xs), list_member1x(Xs,E)). list_member2x([X|Xs],E) :- if_(X=E, list_member1x(Xs,E), list_member2x(Xs,E)). twice(E,Xs) :- list_member2x(Xs,E).
What is it. Run some queries!
?- twice(E,[1,2,3,4,5,2,3,4]). E = 2 ; E = 3 ; E = 4 ; false.
Now something is a little more general:
?- twice(X,[A,B,C,D]). A=X , B=X , dif(C,X), dif(D,X) ; A=X , dif(B,X), C=X , dif(D,X) ; A=X , dif(B,X), dif(C,X), D=X ; dif(A,X), B=X , C=X , dif(D,X) ; dif(A,X), B=X , dif(C,X), D=X ; dif(A,X), dif(B,X), C=X , D=X ; false.
The following are the requests provided by OP:
?- twice(2,[1,2,2,3,4]). true. ?- twice(E,[1,1,2,2,3,3]). E = 1 ; E = 2 ; E = 3 ; false.
Edit
Alternatively, use meta-predicate tcount/3 in combination with (=)/3 as follows:
twice(E,Xs) :- tcount(=(E),Xs,2).