I want to discuss an algorithm here which I found in a data structure book. This book presents a sketch of the algorithm to find the majority element (appears more than N/2 ) in an array of size N . The sketch of the algorithm is as follows:
First, a candidate majority element is found ( this is the harder part ). This candidate is the only element that could possibly be the majority element.The second step determines if this candidate is actually the majority. This is just a sequential search through the array. To find a candidate in the array, A, form a second array B. Then compare A1,A2. If they are equal, add one of these to B; otherwise do nothing. Then compare A3 and A4. Again if they are equal, add one of these to B; otherwise do nothing. Continue in this fashion until the entire array is read. Then recursively find a candidate for B; this is the candidate for A.
I figured out if the N is even, algorithm works fine. But what if N is odd ? How we can handle this case?