Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace MaxSequence
- {
- class MaxSequence
- {
- static void Main(string[] args)
- {
- //explanation what the program does
- Console.WriteLine("This program finds the maximal sequence of equal elements in an array");
- //choice for type of array
- Console.Write("Choose the type of array. Enter str for strings or int for integers: ");
- string choice = Console.ReadLine();
- //Input validation
- while (choice != "str" && choice != "int")
- {
- Console.Write("Invalid input. Please try again: ");
- choice = Console.ReadLine();
- }
- //size of the array
- Console.Write("Enter the number of elements the array will contain: ");
- int len = int.Parse(Console.ReadLine());
- //variables for measuring the sequence
- int seqence = 0, maxSequence = 0;
- //variable to hold the element
- object item = 0;
- if (choice == "str")
- {
- //create array
- string[] str = new string[len];
- //Input
- Console.WriteLine("Enter array elements:");
- for (int i = 0; i < len; i++)
- {
- str[i] = Console.ReadLine();
- }
- //Sort array
- Array.Sort(str);
- //search for longest sequence
- for (int i = 0; i < len; i++)
- {
- seqence = 1;
- for (int j = i+1; j < len; j++)
- {
- if (str[i] == str[j])
- {
- seqence++;
- }
- else
- {
- break;
- }
- }
- if (maxSequence < seqence)
- {
- maxSequence = seqence;
- item = str[i];
- }
- if (maxSequence > len)
- {
- break;
- }
- }
- //display max sequence
- Console.Write("{");
- for (int i = 0; i < maxSequence-1; i++)
- {
- Console.Write("{0}, ", item);
- }
- Console.Write(item);
- Console.WriteLine("}");
- }
- else
- {
- //create array
- int[] ints = new int[len];
- //input
- Console.WriteLine("Enter array elements:");
- for (int i = 0; i < len; i++)
- {
- ints[i] = int.Parse(Console.ReadLine());
- }
- //Sort array
- Array.Sort(ints);
- //search for longest sequence
- for (int i = 0; i < len; i++)
- {
- seqence = 1;
- for (int j = i + 1; j < len; j++)
- {
- if (ints[i] == ints[j])
- {
- seqence++;
- }
- else
- {
- break;
- }
- }
- if (maxSequence < seqence)
- {
- maxSequence = seqence;
- item = ints[i];
- }
- if (maxSequence > len - i)
- {
- break;
- }
- }
- //display max sequence
- Console.Write("{");
- for (int i = 0; i < maxSequence - 1; i++)
- {
- Console.Write("{0}, ", item);
- }
- Console.Write(item);
- Console.WriteLine("}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement