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

Leetcode Problem 260. Single Number III

260. Single Number III

Leetcode Solutions

Approach: Two bitmasks

  1. Initialize bitmask to 0 and XOR all numbers in nums to find the XOR of the two unique numbers.
  2. Set diff to bitmask & (-bitmask) to isolate the rightmost 1-bit.
  3. Initialize two variables x and y to 0. These will hold the two unique numbers.
  4. Iterate over nums again and for each number num, check if num & diff is nonzero.
  5. If num & diff is nonzero, XOR num with x. Otherwise, XOR num with y.
  6. After the loop, x and y will be the two unique numbers.
  7. Return [x, y].
UML Thumbnail

Approach: Hashmap

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...