Skip to content

Instantly share code, notes, and snippets.

@smly
Created July 29, 2011 02:34
Show Gist options
  • Select an option

  • Save smly/1113021 to your computer and use it in GitHub Desktop.

Select an option

Save smly/1113021 to your computer and use it in GitHub Desktop.
majority
#include <stdio.h>
int main()
{
char curr, next, ans;
unsigned int cnt;
for (curr = getchar();;curr = next) {
if ((next = getchar()) == EOF || next == '\n') break;
if (next == curr) {
if (cnt == 0) ans = next;
if (next == ans) {
cnt++;
} else {
if (cnt > 0) cnt--;
if (cnt == 0) ans = curr;
}
}
}
printf("Output: %c\n", (cnt > 0) ? ans : curr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment