bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 137. Single Number II

137. Single Number II

Leetcode Solutions

Bit Manipulation

  1. Initialize two variables ones and twos to 0. These variables will hold the bits that have appeared once and twice respectively.
  2. Iterate through each number in the array nums. a. Update ones with the current number using XOR to add bits that are appearing for the first time and remove bits that are appearing for the second time. b. Update twos with the current number using XOR to add bits that are appearing for the second time and remove bits that are appearing for the third time. c. Use a mask to clear bits that have appeared three times from both ones and twos.
  3. Return ones, which now contains the bits of the unique number.
UML Thumbnail

Hash Map

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...