Single Number
Single Number is a easy bit manipulation problem solved with the missing / repeated numbers pattern.
The best approach, optimal (xor all), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from hash set up.
Problem
In a non-empty array, every value appears twice except one. Find that one value in linear time and constant space.
Examples
Example 1
- Input
nums = [4, 1, 2, 1, 2]- Output
4
Example 2
- Input
nums = [7]- Output
7
Constraints
1 <= nums.length <= 3 * 10^4- Every element appears twice except one. O(n) time, O(1) space.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Hash set | O(n) | O(n) |
| Optimal (XOR all) | O(n) | O(1) |
1Hash set
O(n)O(n)Add values you see for the first time; remove them the second time. One value is left.
- Toggle membership in a set; return the remaining element.
class Solution {
public int singleNumber(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int x : nums) if (!s.add(x)) s.remove(x);
return s.iterator().next();
}
}class Solution:
def singleNumber(self, nums: List[int]) -> int:
s = set()
for x in nums:
if x in s:
s.remove(x)
else:
s.add(x)
return next(iter(s))class Solution {
public:
int singleNumber(vector<int>& nums) {
unordered_set<int> s;
for (int x : nums) if (!s.insert(x).second) s.erase(x);
return *s.begin();
}
};function singleNumber(nums) {
const s = new Set();
for (const x of nums) {
if (s.has(x)) s.delete(x);
else s.add(x);
}
return s.values().next().value;
}2Optimal (XOR all)
O(n)O(1)Every pair cancels to 0 and XOR is order-independent, so the XOR of the whole array is the single value.
- x = 0; x ^= each value.
class Solution {
public int singleNumber(int[] nums) {
int x = 0;
for (int v : nums) x ^= v;
return x;
}
}class Solution:
def singleNumber(self, nums: List[int]) -> int:
x = 0
for v in nums:
x ^= v
return xclass Solution {
public:
int singleNumber(vector<int>& nums) {
int x = 0;
for (int v : nums) x ^= v;
return x;
}
};function singleNumber(nums) {
let x = 0;
for (const v of nums) x ^= v;
return x;
}Edge cases to test
- Negative numbers
- Single element
Hints
Hint 1
x ^ x = 0, and XOR does not care about order.
FAQ
What is the best time complexity for Single Number?
Optimal (XOR all) runs in O(n) time and O(1) extra space.
Which pattern does Single Number use?
It is a bit manipulation problem that uses the missing / repeated numbers pattern. Other problems with the same pattern: Missing Number, Two numbers with odd occurrences.
Is there a brute force solution for Single Number?
Yes. Hash set takes O(n) time and O(n) space. Add values you see for the first time; remove them the second time.
Which edge cases should I test for Single Number?
Negative numbers; Single element.