
Problem
Solution
Submissions
Find Single Number in Array
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 5
Write a JavaScript program to find the single number in an array where every other number appears exactly twice except for one number that appears only once. The solution should have linear time complexity and constant space complexity.
Example 1
- Input: nums = [2, 2, 1]
- Output: 1
- Explanation: Array contains [2, 2, 1]. Number 2 appears twice. Number 1 appears only once. Therefore, the single number is 1.
Example 2
- Input: nums = [4, 1, 2, 1, 2]
- Output: 4
- Explanation: Array contains [4, 1, 2, 1, 2]. Number 1 appears twice (positions 1 and 3). Number 2 appears twice (positions 2 and 4). Number 4 appears only once (position 0). Therefore, the single number is 4.
Constraints
- 1 ≤ nums.length ≤ 30000
- Each element in the array appears twice except for one
- All numbers are integers
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use XOR (exclusive OR) operation properties
- XOR of a number with itself is 0 (a ^ a = 0)
- XOR of a number with 0 is the number itself (a ^ 0 = a)
- XOR operation is commutative and associative
- Iterate through array and XOR all elements together