↓Skip to main content
  1. LeetCode/

LeetCode 137: Single Number II

·2 mins· ·
LeetCode Medium Array Bit-Manipulation
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-10

  • Submissions: Accepted (first try)

Approach
#

XOR cancels pairs only. Count per bit instead (Bit Manipulation): summing bit b over all numbers, each triple contributes a multiple of 3, so count % 3 is the answer’s bit. Python integers are unbounded, so work in 32 bits: if bit 31 is set the answer is negative, so subtract 2^32.

My Attempts
#

Passed on the first try by sorting and checking groups of three with nums[i] != nums[i + 2], the first broken group holding the answer; sorting is O(n log n), missing the linear-time requirement.

Solution
#

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        res = 0
        for b in range(32):
            if sum((x >> b) & 1 for x in nums) % 3:
                res |= 1 << b
        return res - (1 << 32) if res >> 31 else res

Complexity
#

Time O(32n), space O(1).

Mistakes to Avoid
#

  • Python’s right shift of a negative fills with 1s, so (x >> b) & 1 for b < 32 is exactly the two’s-complement bit.
  • Convert a result with bit 31 set back to a negative number.

Interview Explanation
#

Since the others appear three times, I count each bit’s 1s across all numbers and take it modulo 3 to get the answer’s bit; with unbounded Python integers, a result with bit 31 set is converted back to a negative. Time O(32n), space O(1).

Related

LeetCode 287: Find the Duplicate Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Bit-Manipulation
LeetCode 287 note: treat i -> nums[i] as a linked list; the duplicate is the cycle entry, found with Floyd’s algorithm in O(1) space without modifying the array
LeetCode 78: Subsets
·1 min
LeetCode Medium Array Backtracking Bit-Manipulation
LeetCode 78 note: each element is either taken or not; iteratively, each new number copies every existing subset with the number appended
LeetCode 90: Subsets II
·2 mins
LeetCode Medium Array Backtracking Bit-Manipulation
LeetCode 90 note: sort first; for a number equal to the previous one, extend only the subsets created in the previous step
LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
·1 min
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 106 note: the last post-order value is the root; taking roots from the end of the post-order, build the right subtree before the left
LeetCode 48: Rotate Image
·2 mins
LeetCode Medium Array Math Matrix
LeetCode 48 note: rotating 90 degrees clockwise is a transpose followed by reversing each row, both in place
LeetCode 417: Pacific Atlantic Water Flow
·3 mins
LeetCode Medium Array Depth-First-Search Breadth-First-Search Matrix
LeetCode 417 note: walk inward from each ocean’s shore to cells no lower than the current one; cells reached from both oceans are the answer