title: Moore's Voting Algorithm to Find Majority Element
body: <p>I understand that Moore's Voting algorithm to find the majority element has 2 parts - </p>
  
  <ol>
  <li>First part of running Moore's Voting algorithm only gives you A candidate which occurs "most" of the time in the given array. Notice the "most" here.</li>
  <li>In the second part, we need to iterate over the array once again to determine if this candidate occurs maximum number of times (i.e. greater than size/2 times).
  First iteration is to find the candidate &amp; second iteration is to check if this element occurs majority of times in the given array.</li>
  </ol>
  
  <p>So time complexity is: O(n) + O(n).</p>
  
  <p>But I just thought instead of iterating again over the array to find if it occurs more than Array Size/2 times, cant we do something as below ?</p>
  
  <p>I am using maxOcc to keep track of the current maximum element. At the end if maxOcc > size/2 then our candidate is the maximum element. This way we needn't iterate again over the entire array as per the second part of the algorithm.
  Please let me know if this is good or if I am missing something?</p>
  
  <pre><code>void findMajorityElement()
  {
      int arr[] = {10,8,8,8,8,8,8,10};
      int arrSize = 8;
      int mi = 0;
      int occ = 1;
      int maxOcc = 1;
      for(int i=1; i&lt;arrSize-1; ++i)
      {
          if(arr[mi]==arr[i])
          {
              ++occ;
              ++maxOcc;
          }
          else
              --occ;
  
          if(occ == 0)
          {
              mi = i;
              occ = 1;
              maxOcc = 1;
          }
      }
  
      if(maxOcc &gt; arrSize/2)
          cout &lt;&lt;"Majority element is "&lt;&lt;arr[mi]&lt;&lt;endl;
      else
          cout &lt;&lt;"Not Found!"&lt;&lt;endl;
  }
  </code></pre>
  
  <p>This prints Majority element is 8 as it occurs 6 times.
  So we save the other O(n) iteration over the array required in second step.</p>
  
  <p>Kindly let me know if I am missing something?</p>