I have a list of type "Activity". And I need to find the most occurring element in that list. For example:
Activity a = new Activity();
a.Location = "USA";
Activity b = new Activity();
b.Location = "Sri Lanka";
Activity b = new Activity();
b.Location = "USA";
List<Activity> activityList = new List<Activity>();
activityList.add(a);
//......adding the rest of the objects as well.
Now I need to find the most occurring Location in this list. For example, the most occurring Location in the example above would be: USA.
I tried this:
String currentLocation = "";
String mostOccurring = "";
int currentCount = 0;
int highest = 0;
for (int i = 0; i < activityList.Count; i++)
{
currentLocation = activityList[i].Location;
foreach (Activity activity in activityList)
{
if (activity.Location.Equals(currentLocation))
{
currentCount++;
highest = currentCount;
//compare the highest count
}
}
}
But I got stuck and looks like it's not very efficient. It's in a ASP.NET web project, so efficiency is very important I think :) What is the most efficient way to get this done?