Panglot

Longest Area In Array

Apr 9th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2. class LongestArea
  3. {
  4.     static void Main()
  5.     {
  6.         Console.Write("Enter how much words you will input: ");
  7.         int n = int.Parse(Console.ReadLine());
  8.  
  9.         string[] arr = new string[n];
  10.  
  11.         for (int i = 0; i < arr.Length; i++)
  12.         {
  13.             Console.Write("Enter word {0}: ", i + 1);
  14.             arr[i] = Console.ReadLine();
  15.         }
  16.  
  17.         int longest = 0;
  18.         int currElem = 0;
  19.  
  20.  
  21.         for (int i = 0; i < arr.Length; i++)
  22.         {
  23.             int counter = 0;
  24.             for (int j = i; j < arr.Length; j++)
  25.             {
  26.                 if (arr[i] == arr[j])
  27.                     counter++;
  28.             }
  29.             if (counter > longest)
  30.             {
  31.                 longest = counter;
  32.                 currElem = i;
  33.             }
  34.  
  35.         }
  36.  
  37.         Console.WriteLine("The longest sequence of words is: "+longest);
  38.         for (int i = 0; i < arr.Length; i++)
  39.         {
  40.             if(arr[i]==arr[currElem])
  41.                 Console.WriteLine(arr[i]);
  42.         }
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment