Skip to content

Single Number II

Find the single number that appears once when every other number appears three times.

You need to solve this problem efficiently.

InputOutputExplanation
Example 1Result 1Explanation for example 1
Example 2Result 2Explanation for example 2
  • Constraint 1 for Single Number II
  • Constraint 2
  • Constraint 3
ApproachTimeSpaceBest When
Bit CountO(n)O(1)When applicable
HashmapO(n)O(1)When applicable
Ones TwosO(n)O(1)When applicable
★ Recommended

This approach provides an efficient solution for single number ii.

⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
single_number_ii_bit_count.py
from typing import List
def single_number_ii_bit_count(nums: List[int]) -> int:
"""Find single number when others appear three times using bit counting."""
bit_counts = [0] * 32
for num in nums:
for i in range(32):
if num & (1 << i):
bit_counts[i] += 1
result = 0
for i in range(32):
if bit_counts[i] % 3:
result |= (1 << i)
# Handle negative numbers (two's complement)
if result >= 2**31:
result -= 2**32
return result
# Test cases
print(single_number_ii_bit_count([2, 2, 3, 2])) # 3
🎯 Interview Favourite

This approach provides an efficient solution for single number ii.

⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
single_number_ii_hashmap.py
# Python Solution
# Problem: single-number-ii
# Approach: hashmap
# Implementation placeholder for single_number_ii_hashmap
# This file is auto-generated and should contain the solution code.
def single_number_ii_hashmap():
pass
if __name__ == "__main__":
pass
✓ Simple

This approach provides an efficient solution for single number ii.

⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
single_number_ii_ones_twos.py
# Python Solution
# Problem: single-number-ii
# Approach: ones_twos
# Implementation placeholder for single_number_ii_ones_twos
# This file is auto-generated and should contain the solution code.
def single_number_ii_ones_twos():
pass
if __name__ == "__main__":
pass