Calculate the positive min difference between 2 values in a array using Dictionary

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace CSharpGitHubPgm  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.   
  14.             int[] arr = {1,9,4,3};  
  15.             Dictionary<stringint> storeDiffbetweenNumbers = new Dictionary<stringint>();  
  16.             Console.WriteLine("To find the minimum difference between the array");  
  17.             for (int i = 0; i < arr.Length; i++)  
  18.             {  
  19.                 for (int j = i+1; j < arr.Length; j++)  
  20.                 {  
  21.                     int diff = arr[i] - arr[j];  
  22.                     string key = Convert.ToString(arr[i] +"-"+ arr[j]);  
  23.                     storeDiffbetweenNumbers.Add(key, diff);  
  24.                 }  
  25.             }  
  26.   
  27.             var t = storeDiffbetweenNumbers.Where(x => x.Value > 0).Select(x => x).Min(x=>x.Value);  
  28.             Console.WriteLine(t);  
  29.             Console.ReadLine();  
  30.         }  
  31.     }  
  32. }