You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To solve this problem using only constant space, you have to rethink how the numbers are being represented in computers -- using bits.
If you sum the ith bit of all numbers and mod 3, it must be either 0 or 1 due to the constraint of this problem where each number must appear either three times or once. This will be the ith bit of that "single number".
A straightforward implementation is to use an array of size 32 to keep track of the total count of ith bit.
We can improve this based on the previous solution using three bitmask variables:
ones: as a bitmask to represent the ith bit had appeared once.
twos: as a bitmask to represent the ith bit had appeared twice.
threes: as a bitmask to represent the ith bit had appeared three times.