title: How do i remove duplicates from this recursion code? Many 6-sided dice will be rolled and print only unique combinations that add up to a desired sum
body: <p>in the following code, I'm able to print all the dice combinations that add up to a desired sum. I'd like some help in tweaking the code to remove the duplicate entries.</p>
  
  <pre><code>void rep(int n, string ans, int currentSum, int targetSum)
  {
      if(n==0)
      {
          if(currentSum==targetSum)
              cout &lt;&lt; "{" &lt;&lt; ans &lt;&lt; "}"&lt;&lt;endl;
      }
      else
      {
          for(int i=1 ; i&lt;=6 ; i++)
          {
              if(n&gt;1)
              {
                    rep(n-1, ans + to_string(i) + ", ", currentSum+i, targetSum);
              }
              else
              {
                  rep(n-1, ans + to_string(i), currentSum+i, targetSum);
              }
          }
      }
  }
  
  
  void ManyDiceSum(int howManyDice, int targetSum)
  {
      if(howManyDice&gt;0)
          rep(howManyDice,"",0,targetSum);
  }
  
  int main()
  {
      int howManyDice, targetSum;
  
      howManyDice=3;
      targetSum=7;
  
      ManyDiceSum(howManyDice, targetSum);
  
      return 0;
  }
  </code></pre>
  
  <p>Output for 3 dice and target sum of 7 should be:</p>
  
  <pre><code>{1, 1, 5}
  {1, 2, 4}
  {1, 3, 3}
  {2, 2, 3}
  </code></pre>
  
  <p>But, my code is displaying all combinations:</p>
  
  <pre><code>{1, 1, 5}
  {1, 2, 4}
  {1, 3, 3}
  {1, 4, 2}
  {1, 5, 1}
  {2, 1, 4}
  {2, 2, 3}
  {2, 3, 2}
  {2, 4, 1}
  {3, 1, 3}
  {3, 2, 2}
  {3, 3, 1}
  {4, 1, 2}
  {4, 2, 1}
  {5, 1, 1}
  </code></pre>
  
  <blockquote>
    <p>Constraints are: <strong>It has to use recursion</strong> and use <strong>only 1 loop</strong> if
    necessary.</p>
  </blockquote>