Title: C# - Check how many times a value appears in array and find indexes
Tags: c#;arrays;indexof
Question: I have to write a simple program where you have to input student names and their average grade and then print out the highest average grade and who it belongs to. There are several topics here on how to find if value appears in array.  The thing i'm struggling with is what to do if there are more than 1 students with the max average grade. 

Here's what I have so far:

```        Console.WriteLine("Enter the overall count of students.");


        int stuCount = Convert.ToInt32(Console.ReadLine());
        string[] name = new string[stuCount];
        double[] avg = new double[stuCount];

        for (int i = 0; i < stuCount; i++)
        {
            Console.WriteLine("Enter the name of student # {0}.", i + 1);
            name[i] = Console.ReadLine();
            Console.WriteLine("Enter the average grade of {0}.", name[i]);
            avg[i] = Convert.ToDouble(Console.ReadLine());
        }

        // Finding the max average
        double maxAvg = avg[0];
        for (int i = 1; i < stuCount; i++)
        {
            if (avg[i] &gt; maxAvg)
            {
                maxAvg = avg[i];
            }
        }

        // Displaying the max average
        Console.WriteLine("The highest average grade is {0}.", maxAvg);
```

So, can I use ```Array.IndexOf()``` method to find multiple indices?

Thank you.

Comment: you can use `Array.IndexOf` but this will only give you the first occurence. You could delete this one and then rerun it (loop).

Comment: You can only use array or also Classes and Lists?

Comment: Use the overload of `Array.IndexOf()` with the `StartIndex` argument to find multiple indices: https://msdn.microsoft.com/en-us/library/3b2fz03t(v=vs.110).aspx

Comment: @Tinwor I have no restrictions.

Comment: Order the collection descending by grade and grab the first people having the same grade.

Comment: Recent and active question on SO which is dealing with the same issue in multiple ways - [SO link](http://stackoverflow.com/questions/38006865/how-can-i-make-my-procedure-for-finding-the-nth-most-frequent-element-in-an-arra/38008666#38008666)

Here is the accepted answer: After you've found the highest average just loop again through the averages array to see which ones are the max ones and print the stud using the index.

```Console.WriteLine("The highest average grade is {0}.", maxAvg);

Console.WriteLine("The students with this average grade are");
        for (int i = 0; i < stuCount; i++)
        {
            if (avg[i] == maxAvg)
            {
                 Console.WriteLine(name[i]);
            }
        }
```

Comment for this answer: Don't worry. It happens to me all day.

Here is another answer: Another way is creating a class containing two properties (```name``` and ```average grade```), then generate a ```List``` and fill it in a ```for``` cycle. The next step is ```order by descending``` the list through ```average grade``` and select the first N element equals. After that you can simply print the result using a ```ForEach```


Here is another answer: Consider using a class to represent the grades as so;

```class Grade {
    public String Name {get;set;}
    public double Average {get;set;}
}
```

Then your code can be more like;

```Console.WriteLine("Enter the overall count of students.");

        int stuCount = Convert.ToInt32(Console.ReadLine());         

        List<Grade&gt; allGrades = new List<Grade&gt;();

        for (int i = 0; i < stuCount; i++)
        {
            Console.WriteLine("Enter the name of student # {0}.", i + 1);
            var name = Console.ReadLine();
            Console.WriteLine("Enter the average grade of {0}.", name[i]);
            var avg = Convert.ToDouble(Console.ReadLine());
            Grade current = new Grade(){
                Name = name, 
                Average = avg
            };
            allGrades.Add(current);
        }

        // Finding the max average
        double maxAvg = allGrades.Max(g =&gt; g.Average);
        var highestGrades = allGrades.Where(g =&gt; g.Average == maxAvg);

        Console.WriteLine("The following Student(s) have the highest average grade:");
        foreach(var grade in highestGrades){
                // Displaying the max average
                Console.WriteLine("Student: {0}. Grade: {1}.", grade.Name, grade.Average);
        }
}
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:06/24/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><arrays><indexof>', 'Title': 'C# - Check how many times a value appears in array and find indexes', 'ViewCount': 304, 'id': 38011993, 'user_AboutMe': None, 'user_AccountId': 8101578.0, 'user_DisplayName': 'Yurieff', 'user_Reputation': 23.0, 'user_Views': 5.0, 'user_id': 6104464.0}}