svetlozar_kirkov

Longest consecutive sequence in array (Exercise)

Sep 30th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleTesting
  6. {
  7.     internal class ConsoleTesting
  8.     {
  9.         private static void Main()
  10.         {
  11.             Console.Write("Enter the length of the array: ");
  12.             int n = int.Parse(Console.ReadLine());
  13.             var arrayInt = new int[n];
  14.             var seqList = new List<string>();
  15.             var seqCounts = new List<int>();
  16.  
  17.             for (int i = 0; i < arrayInt.Length; i++)
  18.             {
  19.                 Console.Write("Index \"{0}\": ", i);
  20.                 arrayInt[i] = int.Parse(Console.ReadLine());
  21.             }
  22.  
  23.             for (int i = 0; i < arrayInt.Length;)
  24.             {
  25.                 int temp = arrayInt[i];
  26.                 var tempSeq = new List<string>();
  27.                 tempSeq.Add(Convert.ToString(temp));
  28.                 while (i + 1 < arrayInt.Length && arrayInt[i + 1] == temp)
  29.                 {
  30.                     tempSeq.Add(Convert.ToString(arrayInt[i + 1]));
  31.                     i++;
  32.                 }
  33.                 if (tempSeq.Count > 1)
  34.                 {
  35.                     string tempovSeq = string.Join(",", tempSeq);
  36.                     seqList.Add(tempovSeq);
  37.                     seqCounts.Add(tempSeq.Count);
  38.                     i++;
  39.                 }
  40.                 else
  41.                 {
  42.                     i++;
  43.                 }
  44.             }
  45.  
  46.             if (seqList.Count == 0)
  47.             {
  48.                 Console.WriteLine();
  49.                 Console.WriteLine("No consecutive sequences with at least two members found! Exiting...");
  50.                 Console.WriteLine();
  51.                 return;
  52.             }
  53.             else if (seqList.Count == 1)
  54.             {
  55.                 Console.WriteLine();
  56.                 Console.WriteLine("Only one sequence with at least two members found: " + seqList[0]);
  57.                 Console.WriteLine();
  58.                 return;
  59.             }
  60.             Console.WriteLine();
  61.             Console.WriteLine("Consecutive sequences with at least two members: ");
  62.             foreach (string seq in seqList)
  63.             {
  64.                 Console.WriteLine(seq);
  65.             }
  66.             Console.WriteLine();
  67.  
  68.             var allAreSame = seqCounts.Distinct().Count() == 1;
  69.            
  70.             if (allAreSame==true)
  71.             {
  72.                 Console.WriteLine("All sequences have equal length");
  73.                 Console.WriteLine();
  74.                 return;
  75.             }
  76.             else
  77.             {
  78.                 int maxIndex = seqCounts.IndexOf(seqCounts.Max());
  79.                 Console.WriteLine("The longest sequence is: \"{0}\"", seqList[maxIndex]);
  80.                 Console.WriteLine();
  81.                 return;
  82.             }
  83.            
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment