Wednesday, August 1, 2018

Sample C# program to get all possible pairs in a list

This is a sample C# program to get possible pairs from a list of integer values. Duplicate and reverse pair values would be discarded in the program.
  1. using System;
  2. using System.Collections.Generic;

  3. namespace PairFromList
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             var items = new List<int>() { 1, 2, 3, 4, 5 };
  10.          
  11.             for (var i = 0; i < items.Count - 1; i++)
  12.             {
  13.                 for (var j = i + 1; j < items.Count; j++)
  14.                 {
  15.                     Console.WriteLine(items[i] + "-" + items[j]);
  16.                 }
  17.             }

  18.             Console.Read();
  19.         }
  20.     }
  21. }
  22.  
Output :
  1. 1-2
  2. 1-3
  3. 1-4
  4. 1-5
  5. 2-3
  6. 2-4
  7. 2-5
  8. 3-4
  9. 3-5
  10. 4-5