Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created June 11, 2014 22:55
Show Gist options
  • Save gabhi/99914454da82938b0671 to your computer and use it in GitHub Desktop.
Save gabhi/99914454da82938b0671 to your computer and use it in GitHub Desktop.
Majority Element
findCandidate(a[], size)
1. Initialize index and count of majority element
maj_index = 0, count = 1
2. Loop for i = 1 to size – 1
(a)If a[maj_index] == a[i]
count++
(b)Else
count--;
(c)If count == 0
maj_index = i;
count = 1
3. Return a[maj_index]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment