title: Given a list of elements, replicate each element N times
body: <p>I have to replicate each element n times like this:
  <strong>?- replicate([a,b,c],2,X).  -> X = [a,a,b,b,c,c]
  ?- replicate([a,b,c],3,X).  -> X = [a,a,a,b,b,b,c,c,c]</strong></p>
  
  <p>I have tried everything with the information I have so far, the only thing I have been able to do is to determine which is the most repeating element like this:</p>
  
  <pre><code>%List of tuples, keeps track of the number of repetitions.
  modify([],X,[(X,1)]).
  modify([(X,Y)|Xs],X,[(X,K)|Xs]):- K is Y+1.
  modify([(Z,Y)|Xs],X,[(Z,Y)|K]):- Z =\= X, modify(Xs,X,K).
  
  highest((X1,Y1),(_,Y2),(X1,Y1)):- Y1 &gt;= Y2.
  highest((_,Y1),(X2,Y2),(X2,Y2)):- Y2 &gt; Y1.
  
  maxR([X],X).
  maxR([X|Xs],K):- maxR(Xs,Z),highest(X,Z,K).
  
  rep([],R,R).
  rep([X|Xs],R,R1):-modify(R,X,R2),rep(Xs,R2,R1).
  
  maxRepeated(X,R):- rep(X,[],K),maxR(K,R).
  
  
  ?- maxRepeated([1,3,3,4,3,2] ,X).
  X = (3, 3) .
  
  ?- maxRepeated([1,2,3,4,5,6] ,X).
  X = (1, 1) .
  </code></pre>