Ad
  • Default User Avatar

    This is the solution I went for :D

    Very surprised this one doesn't have more Best Practices votes.

  • 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!

  • Default User Avatar

    Hi,

    I was fiddling around with this little bit and I have noticed that "nums.pop()" gives the "nums[0]" value.

    For example, when there was more than one values of "nums", nums.pop() will always spit out the first number in the list.

    I thought set.pop() picks randomly.

    Is there something that I am missing?

    Can someone please explain why its not picking out randomely?

    Thank you.

  • Default User Avatar

    TBH i'm being jealous atm, really clever way of thinking

  • Custom User Avatar

    这个方法很聪明!clever!

  • Default User Avatar
  • Custom User Avatar

    yeah, we should have more foresight when coding.
    i love this solution.

  • Custom User Avatar

    I like this better than the counting versions, since it should be more efficient in both space and time especially for very huge sequences.
    I solved it the same way.

  • Custom User Avatar

    I like this one because its very similar to mine :p
    mutual annihilation of equal numbers FTW!