Minimum Excluded Value (MEX)

Mex kind of scares me. I need to write down the common ideas associated with this.

Codeforces problems:

Resources

I have bottleneck when computing MEX

int mex(vector<int> &a) {
    vector<bool> f(a.size() + 1, 0);
    for (int i : a) if (i <= (int) a.size()) f[i] = 1;
    int mex = 0;
    while (f[mex]) ++mex;
    return mex;
}