-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestAreaInArray.cs
More file actions
65 lines (61 loc) · 1.75 KB
/
Copy pathLongestAreaInArray.cs
File metadata and controls
65 lines (61 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class LongestAreaInArray
{
static void Main(string[] args)
{
Console.Write("n= ");
int n = int.Parse(Console.ReadLine());
string[] strings = new string[n];
for (int i = 0; i < n; i++)
{
Console.Write("next element= ");
strings[i] = Console.ReadLine();
}
int counter = 1;
Dictionary<string, int> listOfRepeats = new Dictionary<string, int>();
string nextElement = strings[0];
for (int i = 1; i <= n; i++)
{
if (i == n)
{
listOfRepeats[strings[i - 1]] = counter;
}
else
{
if (strings[i] == nextElement)
{
counter++; listOfRepeats[strings[i]] = counter;
}
else
{
listOfRepeats[nextElement] = counter; counter = 1;
}
nextElement = strings[i];
}
}
int maximumValue = listOfRepeats.Values.Max();
int countValues = 0;
string previousKey;
previousKey = listOfRepeats.Keys.First();
foreach (var item in listOfRepeats)
{
if (countValues > 0)
{
break;
}
if (item.Value == maximumValue)
{
countValues++; previousKey = item.Key;
}
}
Console.WriteLine(maximumValue);
for (int i = 0; i < maximumValue; i++)
{
Console.WriteLine(previousKey);
}
}
}