title: Generate unique ids pairs
body: <p>I'm trying to generate all possible unique ids pairs. Currently I'm trying to achieve it with two like that:</p>
  <pre><code>private prepareNewList(userIds: number[], existedPartnerList: string[]) {
      const newPartnerList = [];
      let usedUsersIds = [];
  
      for (let i = 0; i &lt; userIds.length - 1; i++) {
        for (let j = i + 1; j &lt; userIds.length; j++) {
          if (usedUsersIds.includes(userIds[i])) {
            continue;
          }
  
          const pair = ${userIds[i]}-${userIds[j]};
          const reversedPair = ${userIds[j]}-${userIds[i]};
  
          if (
            !existedPartnerList.includes(pair) &amp;&amp;
            !existedPartnerList.includes(reversedPair)
          ) {
            newPartnerList.push(pair);
            usedUsersIds.push(userIds[i], userIds[j]);
  
            break;
          }
        }
      }
  
      usedUsersIds = [];
  
      for (let i = userIds.length - 1; i &gt; 0; i--) {
        for (let j = i - 1; j &gt;= 0; j--) {
          if (usedUsersIds.includes(userIds[j])) {
            continue;
          }
  
          const pair = ${userIds[i]}-${userIds[j]};
          const reversedPair = ${userIds[j]}-${userIds[i]};
  
          if (
            !existedPartnerList.includes(pair) &amp;&amp;
            !existedPartnerList.includes(reversedPair) &amp;&amp;
            !newPartnerList.includes(pair) &amp;&amp;
            !newPartnerList.includes(reversedPair)
          ) {
            newPartnerList.push(pair);
            usedUsersIds.push(userIds[i], userIds[j]);
  
            break;
          }
        }
      }
  
      return this.filterPairs(newPartnerList);
    }
  
  private filterPairs(pairs: string[]): string[] {
      const usedIds = [];
      const pairsToRemove = [];
      pairs = pairs.sort(() =&gt; Math.random() - 0.5);
  
      for (let i = 0; i &lt; pairs.length; i++) {
        const parsedPair = pairs[i].split('-');
  
        if (parsedPair.some((id) =&gt; usedIds.includes(id))) {
          pairsToRemove.push(pairs[i]);
        }
  
        usedIds.push(...parsedPair);
      }
  
      return pairs.filter((item) =&gt; !pairsToRemove.includes(item));
    }
  </code></pre>
  <p>But it doesn't work properly. When I run the script for a first time I get <code>['1-2', '3-4', '5-6', '6-4', '5-3', '4-2', '3-1']</code> from first method and <code>[ '1-2', '3-4', '5-6' ]</code> after filtration, which is correct (because, for example, numbers from '6-4' pair have already been used in '3-4' and '5-6).</p>
  <p>When I run the script for a second time I get <code>[ '1-3', '2-3', '4-5', '6-4', '5-3', '4-2' ]</code> from first method and  <code>[ '1-3', '4-5' ]</code> after filtration, which isn't correct, because there is one pair left <code>[ '6-2' ]</code> or <code>[ '2-6' ]</code>.</p>
  <p>So my method doesn't work as I expect. What should I do to get this working?</p>
  <p><strong>EDIT 1:</strong></p>
  <p>I want to run this script every month to generate unique pairs of users. For example, in this month I've create pair 'user 1- user 2'. So in this month I can't use user 1 and user 2 anymore and can't use pair 'user 1- user 2' forever</p>
  <p><strong>EDIT 2:</strong></p>
  <p>Perhaps I explained incorrectly or not quite accurately. Every month I have to generate unique user pairs. For example, I have 6 users (there is always an even number), at the first start I can generate them as I want, because the table in the database is empty. For example: 1-2, 3-4, 5-6. The next month I run the script again and, suppose, the number of users has not changed, the existing pairs (1-2, 3-4, 5-6) are taken from the base and based on these existing pairs I must create new ones that will not be duplicates ( e.g. 1-4, 2-6, 3-5). And so every month + users will be added and, accordingly, the yield will increase.</p>