I know this kind of question is asked before, but I am a noob and cannot understand where the array is out of bounds.
P.S this is not the exact duplicate of the every " system.IndexOutOfRange exception in C# " as the situation and the context of the problem is totally different from each other.So please read the question first before reporting it as the " exact duplicate ".
class Program
{
static void Main(string[] args)
{
int highestcount = 0, e = 0, h = 0;
string highest = "", a = "", b = "", j = "";
int y = int.Parse(Console.ReadLine());
e = y - 1;
string[] savArray = new string[e];
for (int count = 0; count < y; count++)
{
var x = Console.ReadLine().Split(' ');
if (count+1 <= y)
{
j = x[count + 1];
savArray[count] = j;
}
}
for (int count1 = 0; count1 < y; count1++)
{
h = 0;
a = savArray[count1];
for (int count2 = 1; count2 < y; count2++)
{
b = savArray[ count2];
if (a == b)
{
h = h + 1;
if (highestcount <= h)
{
highestcount = h;
highest = a;
}
}
}
}
Console.Write(highest);
Console.Read();
}
}
The program is to find the most common sport. First, the user has to enter how many no: of entries are there on the list.
After that, the user has to enter his name and then his favorite sport.
If you read the program what I meant to do is simply save the 2nd value of x in the array because every second value is the sport which the user has given, as I want to save the sport's name to check which sport is more common.
But the exception is ruining the simple idea and I am stuck with it, please help.