Exactly one pair in the Prolog list

Does anyone know how I can determine / verify that there is exactly one duplicate item in the break list?

I am studying a test.

0


source to share


2 answers


Sorting the list with sort/2

. It removes duplicates, so if the sorted list is exactly one short, you have exactly one pair.

one_duplicate(L) :-
    sort(L, Sorted),
    length(L, Len),
    length(Sorted, SortedLen),
    Len =:= SortedLen + 1.

      



Finding a double pair is another question.

+5


source


one_duplicate(L) :-
    sort(L, Sorted),
    length(L, Len),
    length(Sorted, SortedLen),
    Len =:= SortedLen + 1.

      



-2


source







All Articles