Single Number
Problem Statement
Section titled “Problem Statement”Given a non-empty array of integers where every element appears twice except for one, find that single element.
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
- Constraint 2
- Constraint 3
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Xor | O(n) | O(1) | When applicable |
| Hashmap | O(n) | O(1) | When applicable |
| Math | O(n) | O(1) | When applicable |
Approach 1: Xor
Section titled “Approach 1: Xor”This approach provides an efficient solution for single number.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”from typing import List
def single_number_xor(nums: List[int]) -> int: """Find single number using XOR operation.""" result = 0 for num in nums: result ^= num return result
# Test casesprint(single_number_xor([2, 2, 1])) # 1print(single_number_xor([4, 1, 2, 1, 2])) # 4#include <vector>using namespace std;
int singleNumberXor(vector<int>& nums) { int result = 0; for (int num : nums) { result ^= num; } return result;}
int main() { vector<int> nums1 = {2, 2, 1}; cout << singleNumberXor(nums1) << endl; // 1 return 0;}public class SingleNumberXor { public static int singleNumberXor(int[] nums) { int result = 0; for (int num : nums) { result ^= num; } return result; }
public static void main(String[] args) { int[] nums = {2, 2, 1}; System.out.println(singleNumberXor(nums)); // 1 }}function singleNumberXor(nums) { let result = 0; for (const num of nums) { result ^= num; } return result;}
console.log(singleNumberXor([2, 2, 1])); // 1console.log(singleNumberXor([4, 1, 2, 1, 2])); // 4fn single_number_xor(nums: Vec<i32>) -> i32 { let mut result = 0; for num in nums { result ^= num; } result}
fn main() { println!("{}", single_number_xor(vec![2, 2, 1])); // 1 println!("{}", single_number_xor(vec![4, 1, 2, 1, 2])); // 4}package main
import "fmt"
func singleNumberXor(nums []int) int { result := 0 for _, num := range nums { result ^= num } return result}
func main() { fmt.Println(singleNumberXor([]int{2, 2, 1})) // 1 fmt.Println(singleNumberXor([]int{4, 1, 2, 1, 2})) // 4}Approach 2: Hashmap
Section titled “Approach 2: Hashmap”This approach provides an efficient solution for single number.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: single-number# Approach: hashmap
# Implementation placeholder for single_number_hashmap# This file is auto-generated and should contain the solution code.
def single_number_hashmap(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: single-number# Approach: hashmap
# Implementation placeholder for single_number_hashmap# This file is auto-generated and should contain the solution code.
def single_number_hashmap(): pass
if __name__ == "__main__": pass# Java Solution# Problem: single-number# Approach: hashmap
# Implementation placeholder for single_number_hashmap# This file is auto-generated and should contain the solution code.
def single_number_hashmap(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: single-number# Approach: hashmap
# Implementation placeholder for single_number_hashmap# This file is auto-generated and should contain the solution code.
def single_number_hashmap(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: single-number# Approach: hashmap
# Implementation placeholder for single_number_hashmap# This file is auto-generated and should contain the solution code.
def single_number_hashmap(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: single-number# Approach: hashmap
# Implementation placeholder for single_number_hashmap# This file is auto-generated and should contain the solution code.
def single_number_hashmap(): pass
if __name__ == "__main__": passApproach 3: Math
Section titled “Approach 3: Math”This approach provides an efficient solution for single number.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: single-number# Approach: math
# Implementation placeholder for single_number_math# This file is auto-generated and should contain the solution code.
def single_number_math(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: single-number# Approach: math
# Implementation placeholder for single_number_math# This file is auto-generated and should contain the solution code.
def single_number_math(): pass
if __name__ == "__main__": pass# Java Solution# Problem: single-number# Approach: math
# Implementation placeholder for single_number_math# This file is auto-generated and should contain the solution code.
def single_number_math(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: single-number# Approach: math
# Implementation placeholder for single_number_math# This file is auto-generated and should contain the solution code.
def single_number_math(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: single-number# Approach: math
# Implementation placeholder for single_number_math# This file is auto-generated and should contain the solution code.
def single_number_math(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: single-number# Approach: math
# Implementation placeholder for single_number_math# This file is auto-generated and should contain the solution code.
def single_number_math(): pass
if __name__ == "__main__": pass