Single Number II
Problem Statement
Section titled “Problem Statement”Find the single number that appears once when every other number appears three times.
You need to solve this problem efficiently.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
| Example 1 | Result 1 | Explanation for example 1 |
| Example 2 | Result 2 | Explanation for example 2 |
Constraints
Section titled “Constraints”- Constraint 1 for Single Number II
- Constraint 2
- Constraint 3
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Bit Count | O(n) | O(1) | When applicable |
| Hashmap | O(n) | O(1) | When applicable |
| Ones Twos | O(n) | O(1) | When applicable |
Approach 1: Bit Count
Section titled “Approach 1: Bit Count”This approach provides an efficient solution for single number ii.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”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 casesprint(single_number_ii_bit_count([2, 2, 3, 2])) # 3#include <vector>using namespace std;
int singleNumberIIBitCount(vector<int>& nums) { vector<int> bitCounts(32, 0); for (int num : nums) { for (int i = 0; i < 32; i++) { if (num & (1 << i)) { bitCounts[i]++; } } }
int result = 0; for (int i = 0; i < 32; i++) { if (bitCounts[i] % 3) { result |= (1 << i); } }
return result;}
int main() { vector<int> nums = {2, 2, 3, 2}; cout << singleNumberIIBitCount(nums) << endl; // 3 return 0;}public class SingleNumberIIBitCount { public static int singleNumberIIBitCount(int[] nums) { int[] bitCounts = new int[32]; for (int num : nums) { for (int i = 0; i < 32; i++) { if ((num & (1 << i)) != 0) { bitCounts[i]++; } } }
int result = 0; for (int i = 0; i < 32; i++) { if (bitCounts[i] % 3 != 0) { result |= (1 << i); } }
return result; }
public static void main(String[] args) { int[] nums = {2, 2, 3, 2}; System.out.println(singleNumberIIBitCount(nums)); // 3 }}function singleNumberIIBitCount(nums) { const bitCounts = new Array(32).fill(0); for (const num of nums) { for (let i = 0; i < 32; i++) { if (num & (1 << i)) { bitCounts[i]++; } } }
let result = 0; for (let i = 0; i < 32; i++) { if (bitCounts[i] % 3) { result |= (1 << i); } }
if (result >= Math.pow(2, 31)) { result -= Math.pow(2, 32); } return result;}
console.log(singleNumberIIBitCount([2, 2, 3, 2])); // 3fn single_number_ii_bit_count(nums: Vec<i32>) -> i32 { let mut bit_counts = vec![0; 32]; for num in nums { for i in 0..32 { if (num & (1 << i)) != 0 { bit_counts[i] += 1; } } }
let mut result = 0; for i in 0..32 { if bit_counts[i] % 3 != 0 { result |= 1 << i; } }
if result >= (1 << 31) { result -= 1 << 32; } result}
fn main() { println!("{}", single_number_ii_bit_count(vec![2, 2, 3, 2])); // 3}package main
import "fmt"
func singleNumberIIBitCount(nums []int) int { bitCounts := make([]int, 32) for _, num := range nums { for i := 0; i < 32; i++ { if (num & (1 << i)) != 0 { bitCounts[i]++ } } }
result := 0 for i := 0; i < 32; i++ { if bitCounts[i]%3 != 0 { result |= (1 << i) } }
if result >= (1 << 31) { result -= (1 << 32) } return result}
func main() { fmt.Println(singleNumberIIBitCount([]int{2, 2, 3, 2})) // 3}Approach 2: Hashmap
Section titled “Approach 2: Hashmap”This approach provides an efficient solution for single number ii.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# 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# C++ 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# Java 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# JavaScript 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# Rust 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# Golang 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__": passApproach 3: Ones Twos
Section titled “Approach 3: Ones Twos”This approach provides an efficient solution for single number ii.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# 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# C++ 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# Java 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# JavaScript 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# Rust 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# Golang 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