Random commentary about Machine Learning, BigData, Spark, Deep Learning, C++, STL, Boost, Perl, Python, Algorithms, Problem Solving and Web Search
Assume the stream of integers is appearing in array 'a'.Initialize - majority = a[0];counter = 1;As the number comes in (i=1 onwards) -if a[i] == majority -> counter++;else if counter == 0 -> majority = a[i];else counter--;At the end, if there is majority, it is saved in the variable majority.
Assume the stream of integers is appearing in array 'a'.
ReplyDeleteInitialize -
majority = a[0];
counter = 1;
As the number comes in (i=1 onwards) -
if a[i] == majority -> counter++;
else if counter == 0 -> majority = a[i];
else counter--;
At the end, if there is majority, it is saved in the variable majority.