Ad
  • Default User Avatar

    oh yeah i ha trouble understanding too thanks

  • Default User Avatar

    Hello,

    You're right about set.pop() removing and returning a random element. The missing link here is that when you get to the return statement, the set only ever contains one value because of the problem specifications.

    In the for loop, whether an element ultimately ends in or out of the set is determined by the parity (even or odd) of the number of times it appears: the first time it appears, it's added to the set for the first time, then the next time it's removed from the set, then the next time it's added again, and so on.

    In the end, only elements which appear an odd number of times end up in the set, and according to the problem, there's only one such element in the sequence, so it is the only one that could possibly be returned. So essentially, it is choosing randomly, but it only ever has one option.

    Hope this helps!