advanced · bit manipulation · XOR · counting mod 3
The one value not in a triple
Problem
A warehouse scanner logs part numbers. Every part number appears exactly three times, except one that appears exactly once. Find that one in O(n) time and O(1) extra space.
XOR alone does not help here: three copies XOR to the value itself, not to 0.
Constraints
- 1 ≤ n ≤ 31 (visualization limit)
- part numbers from 0 to 999
Examples
[6, 1, 6, 6] → 1
[2, 9, 2, 4, 9, 2, 9] → 4
Show the solution
Two masks as a per-bit counter modulo 3
Each bit position independently counts how many values had a 1 there, modulo 3. Triples bring every count back to 0, so what remains is exactly the bits of the single value.
Why it is correct
Per bit, the pair (twos, ones) moves through the states 00 → 01 → 10 → 00 each time a value with a 1 in that position arrives, and stays put on a 0. The first update sets ones from 0 to 1 only when twos is 0, or clears it; the second update, using the new ones, does the same for twos. After a value has been seen three times, its bits have returned to 00; the single value leaves its 1 bits in state 01, so ones equals it at the end.
Complexity
Time O(n), extra space O(1). Two XORs, two ANDs and two NOTs per value; the masks are two machine words.
For contrast: Count occurrences in a hash map and return the value with count 1. That costs O(n) time and O(n) space.
Edge cases
- A single value only.
- The single value is 0: ones stays 0.
- Triples may be scattered anywhere in the list.
Common mistakes
- Updating twos with the old ones instead of the new one.
- Using XOR of everything, which leaves the XOR of all distinct values.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class SingleAmongTriples
{
/// <summary>Every value appears three times except one: return it. Per-bit counter mod 3 in two masks; O(n) time, O(1) space.</summary>
public static int Find(int[] values)
{
int ones = 0, twos = 0; // per bit: seen once / twice (mod 3)
foreach (int x in values)
{
ones = (ones ^ x) & ~twos;
twos = (twos ^ x) & ~ones;
}
return ones;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/SingleAmongTriples.cs
from collections.abc import Iterable
def find(values: Iterable[int]) -> int:
"""Every value (>= 0) appears three times except one: return it. Per-bit counter mod 3 in two masks; O(n), O(1)."""
ones = twos = 0 # per bit: seen once / twice (mod 3)
for x in values:
ones = (ones ^ x) & ~twos
twos = (twos ^ x) & ~ones
return ones
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/single_among_triples.py
package dev.bekadan.dsa.problems;
public final class SingleAmongTriples {
private SingleAmongTriples() {
}
/** Every value appears three times except one: return it. Per-bit counter mod 3 in two masks; O(n), O(1). */
public static int find(int[] values) {
int ones = 0; // per bit: seen once (mod 3)
int twos = 0; // per bit: seen twice (mod 3)
for (int x : values) {
ones = (ones ^ x) & ~twos;
twos = (twos ^ x) & ~ones;
}
return ones;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/SingleAmongTriples.java
#pragma once
#include <span>
namespace bekadan::dsa::problems {
// Every value appears three times except one: return it. Per-bit counter mod 3 in two masks; O(n), O(1).
inline int single_among_triples(std::span<const int> values) {
unsigned ones = 0, twos = 0; // per bit: seen once / twice (mod 3); unsigned for well-defined bit operations
for (const int value : values) {
const auto x = static_cast<unsigned>(value);
ones = (ones ^ x) & ~twos;
twos = (twos ^ x) & ~ones;
}
return static_cast<int>(ones);
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/single_among_triples.hpp