title: Prolog: find and put into the list duplicates
body: <p>Good day, guys. Can't figure out, why prolog predicate is putting all duplicates into my new list. F.e. I have to pick all duplicates:</p>
  <pre><code>?- duplicates([a, b, a, a, d, d], R). 
  R = [a, d] 
  </code></pre>
  <p>I have wrote this prolog program:</p>
  <pre><code>duplicates([], []).
  duplicates([First|Rest], NewRest) :- 
      not(member(First, Rest)), 
      duplicates(Rest, NewRest).
  duplicates([First|Rest], [First|NewRest]) :- 
      member(First, Rest), 
      duplicates(Rest, NewRest).
  </code></pre>
  <p>But it returns:</p>
  <pre><code>R = [a, a] .
  </code></pre>
  <p>I think I need to put a (!) sign somewhere, but cannot understand, where. Any suggestions?</p>