title: C#: Get Most Occurring Element in a List?
body: <p>I have a list of type "Activity". And I need to find the most occurring element in that list. For example:</p>
  
  <pre><code>Activity a = new Activity();
  a.Location = "USA";
  
  Activity b = new Activity();
  b.Location = "Sri Lanka";
  
  Activity b = new Activity();
  b.Location = "USA";
  
  List&lt;Activity&gt; activityList = new List&lt;Activity&gt;();
  activityList.add(a);
  //......adding the rest of the objects as well.
  </code></pre>
  
  <p>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. </p>
  
  <p>I tried this:</p>
  
  <pre><code>            String currentLocation = "";
              String mostOccurring = "";
              int currentCount = 0;
              int highest = 0;
  
              for (int i = 0; i &lt; activityList.Count; i++)
              {
                  currentLocation = activityList[i].Location;
  
                  foreach (Activity activity in activityList)
                  {
                      if (activity.Location.Equals(currentLocation))
                      {
                          currentCount++;
                          highest = currentCount;
                          //compare the highest count
                      }
                  }
              }
  </code></pre>
  
  <p>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?</p>