Sunday, February 7, 2010

Compute all the items which appears more than 50% of time

Given a stream of symbols, with a finite alphabet, compute all the items which appears more than 50% of time

1 comment:

  1. #include
    using namespace std;

    int main(int argc, char* argv[])
    {
    unsigned int counter = 0, most_freq, n;
    while (cin >> n && n > 0)
    if (!counter)
    counter++, most_freq = n;
    else
    counter += (n == most_freq) ? +1 : -1;
    cout << most_freq << endl;
    }

    ReplyDelete