### Finding the majority element in an input stream

• Problem Description

• Problem Description:

• A (very long) stream of integers contains different values

Example:

 ``` 1 2 4 3 4 7 8 4 4 4 4 9 15 4 4 1 4 4 15 4 98 4 765 4 9825 4 2 4 4 4 2 ... ```

• You are given that some value will occur more than 50% of the time

• Find the majority element in the input data stream

• Note: (restriction)

 You cannot assume any upperbound on the number of integers in the input stream You cannot assume that the number of different integers is finite

• Online Algorithm to find the majority element

• Pseudo Code:

 ``` int x; // next input value int maj; // The current solution int count = 0; // Excess count while ( input stream ≠ empty ) { x = read(input stream); if ( count == 0 ) { maj = x; // New candiate count = 1; // Count of candidate } else { /* ------------------------------------------ count > 0 (we have a current solution) ------------------------------------------ */ if ( x == maj ) { count++; // Increase the certainty that solution is correct } else { count--; // Decrease the certainty that current solution is correct } } } print(maj); ```

Note:

 Run the algorithm on some input sequence to convince yourself that it is correct. You can look at the count variable as the residual votes for some candidate The majority element will always win because it has > 0 residual votes (because it has more than 50% of the votes)