Skip to content

Instantly share code, notes, and snippets.

@ashish173
Last active December 25, 2015 17:48
Show Gist options
  • Save ashish173/7015317 to your computer and use it in GitHub Desktop.
Save ashish173/7015317 to your computer and use it in GitHub Desktop.
moore's voting algo
int count = 1, majorityIndex = 0;
for(int i=1; i<cast.length; i++) {
if(cast[i]==cast[majorityIndex])
count++;
else
count--;
if(count==0) { // no majority at this point
majorityIndex=i;
count=1;
}
}
System.out.println("Majority element is " + cast[majorityIndex]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment