Approach hintTry it first, then revealHide
Put all values in a hash set and start counting only from numbers whose predecessor x - 1 is absent; each run is then walked once, giving O(n) overall.
Put all values in a hash set and start counting only from numbers whose predecessor x - 1 is absent; each run is then walked once, giving O(n) overall.