Given an integer array nums where every element appears exactly three times except for one element which appears exactly once. Find the single element and return it. You must implement a solution with linear runtime and constant extra space.
nums = [2,2,3,2]3nums = [0,1,0,1,0,1,99]99Track each bit's count mod 3 with two bit-arrays: ones and twos. ones holds bits whose cumulative count mod 3 is 1; twos holds bits whose count mod 3 is 2. When a bit appears 3 times it clears from both (count mod 3 = 0). The single number's bits appear only once and never reach count 3, so they remain in ones. The update formula ones = (ones^n) & ~twos and twos = (twos^n) & ~ones implements this 3-state machine atomically for all 32 bits simultaneously.