title: C# - Looping through pairs and matching
body: <p>So I'm stuck on a program where I have a series of pairs that may or may not be able to connect together to form a complete path through the pairs. I need to be able to check if the second item in a pair can match the first item in another pair and so on until there are no pairs left. For example, my pairs might be:</p>
  
  <p>(1,5)<br />
  (2,4)<br />
  (3,2)<br />
  (5,3)<br />
  (4,3)</p>
  
  <p>I would need to be able to somehow iterate through the pairs and check if I can get a complete path that travels through each one, based on if the second digit of a pair matches the first digit of the next pair. In this example, the output would be:<br /><br /> (1,5), (5,3), (3,2), (2,4), (4,3) <br /><br />forming a complete match. If a match can't be formed, I need to report a failure. The input is based on a text file. So far I've been able to read the file with a Streamreader and split the pairs based on newline, then iterate through and split each pair into its items based on the comma. I'm pretty much clueless on how to proceed, if anyone has some ideas I would appreciate it.</p>
  
  <pre><code>StreamReader sr = new StreamReader("inputs.txt");
  string line = null;
  line = sr.ReadToEnd();
  var str = line.Trim().Split('\n');
  int length = str.Length;
  int index=1;
  
  while (index &lt; length)
  {
      var pair = str[index].Split(',');
      var item1 = pair[0];
      var item2 = pair[1];
  }
  </code></pre>