Lv.1arraybit-manipulationmath
The Unpaired Sock
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Every value in socks appears exactly twice, except one value that appears exactly once. Return that value. The
values are in no particular order.
Examples
| socks | result |
|---|---|
[2, 2, 1] | 1 |
[4, 1, 2, 1, 2] | 4 |
[-7] | -7 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(socks: int[]) → int
Constraints
-
socks.length(always odd) -
socks[i] - Exactly one value appears once; every other value appears exactly twice.
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| socks | Expected |
|---|---|
[2,2,1] | 1 |
[4,1,2,1,2] | 4 |
[-7] | -7 |
Hint
Hint
x ^ x = 0, x ^ 0 = x, and XOR is commutative and associative, so XOR-ing every value cancels the pairs and leaves the single
one (negative numbers work the same bitwise). Counting each value with a full scan is and exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1124 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 3 | verdict only |