title: Best way to Uniquely Pair Array Elements (where elements are primitive types and arrays)
body: <p>I was recently given the following challenge to complete in 30 minutes:</p>
  <p><em>From a collection of tennis players create unique teams of 2. Once the teams are in place create a list of potential tennis matches between all the teams; with no duplicates matches.</em></p>
  <p>My answer below works, but due to time constraints I had to brute force it. It appears there's some concept I'm missing. At the very least there's definitely a more efficient way to code it. I'll be tweaking it on my <a href="https://codepen.io/ryannmedina/pen/WNRPoaV" rel="nofollow noreferrer">codepen here</a>.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const players = ["Mickey", "Donald", "Goofy", "Minny", "Woody"];
  
  const makeTeams = (players) =&gt; {
    let teams = [];
    let i = 0;
    
    players.forEach((player) =&gt; {  
      for (let j = i; j &lt; players.length; j++) {
        let team = [player];
  
        if (player !== players[j]) {
          team.push(players[j]);
          teams.push(team);
        }
      }
  
      i++;
    });
  
    return teams;
  };
  
  const makeMatches = (teams) =&gt; {
    let matches = [];
    let i = 0;
  
    teams.forEach((team) =&gt; {
      for (let j = i; j &lt; teams.length; j++) {
        let match = [team];
  
        let sameTeam = team[0] === teams[j][0] &amp;&amp; team[1] === teams[j][1];
        let samePlayers = team[0] === teams[j][0] || team[1] === teams[j][0] || team[0] === teams[j][1] || team[1] === teams[j][1];
  
        if (!sameTeam &amp;&amp; !samePlayers) {
          match.push(teams[j]);
          matches.push(match);
        }
      }
  
      i++;
    });
  
    return matches;
  };
  
  // Display Teams
  const teams = makeTeams(players);
  
  const teamsList = document.getElementById("teams");
  
  teams.forEach((team) =&gt; {
    let el = document.createElement("li");
  
    el.innerText = team[0] + " and " + team[1];
  
    teamsList.appendChild(el);
  });
  
  // Display Matches
  const matches = makeMatches(teams);
  
  const matchList = document.getElementById("matches");
  
  matches.forEach((match) =&gt; {
    let el = document.createElement("li");
  
    let team1 = match[0][0] + " and " + match[0][1];
    let team2 = match[1][0] + " and " + match[1][1];
  
    el.innerText = team1 + " vs " + team2;
  
    matchList.appendChild(el);
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;h2&gt;Teams&lt;/h2&gt;
  &lt;ul id="teams"&gt;&lt;/ul&gt;
  
  &lt;h2&gt;Matches&lt;/h2&gt;
  &lt;ul id="matches"&gt;&lt;/ul&gt;</code></pre>
  </div>
  </div>
  </p>