title: C++ Looking for the Element with the highest occurrence in an array
body: <p>I'm looking for an elegant way of determining which element has the highest occurrence (mode) in a C++ ptr array.</p>
  
  <p>For example, in</p>
  
  <pre><code>{"pear", "apple", "orange", "apple"}
  </code></pre>
  
  <p>the <code>"apple"</code> element is the most frequent one.</p>
  
  <p>My previous attempts have failed
  EDIT: The array has already been sorted.</p>
  
  <pre><code>int getMode(int *students,int size)
  {
      int mode;
      int count=0, 
      maxCount=0,
      preVal;
  
      preVal=students[0]; //preVall holds current mode number being compared
      count=1;
      for(int i =0; i&lt;size; i++) //Check each number in the array
      {
          if(students[i]==preVal) //checks if current mode is seen again
          {
              count++; //The amount of times current mode number has been seen.
              if(maxCount&lt;count)  //if the amount of times mode has been seen is more than maxcount
              {
                  maxCount=count; //the larger it mode that has been seen is now the maxCount
                  mode=students[i]; //The current array item will become the mode
              }else{
                  preVal = students[i];
                  count = 1;
              }
  
          }
  
      }
  
      return mode; 
  }
  </code></pre>