Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace TopTwoNumber
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] array = new int[] { 2, 6, 1, 9, 4, 5, 3, 3 };
- var sortedArray = array.OrderBy(x => x).ToArray(); // 1 2 3 3 4 5 6 9
- List<int> listWhithUnique = new List<int>(); // 1 2 3 4 5 6 9
- // Insert repeated elements only once
- for (int i = 1; i < sortedArray.Length; i++)
- {
- if (sortedArray[i] != sortedArray[i - 1])
- listWhithUnique.Add(sortedArray[i-1]);
- }
- if(sortedArray[sortedArray.Length-1] != sortedArray[sortedArray.Length-2])
- {
- listWhithUnique.Add(sortedArray[sortedArray.Length - 1]);
- }
- // Find the maximum length by traversing the array
- int count = 0;
- int result = 0;
- for (int i = 0; i < listWhithUnique.Count; i++)
- {
- if (i > 0 && listWhithUnique[i] == listWhithUnique[i - 1] + 1)
- {
- count++;
- }
- else
- {
- count = 1;
- }
- result = Math.Max(result, count);
- }
- Console.WriteLine(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement