Find Unique Element in Java
Problem
In an array where every value appears exactly twice except one, that one value is the unique element — the only one without a matching pair.
Given an array where every element appears exactly twice except for one, find that one unique element.
Java Program
public class FindUniqueElement {
public static void main(String[] args) {
int[] arr = {7, 3, 5, 3, 7};
int result = 0;
// Every value that appears twice cancels itself out via XOR
for (int num : arr) {
result ^= num;
}
System.out.println("Unique element: " + result);
}
}Output
Core Logic
XOR-ing every element together cancels out every value that appears twice, leaving only the one that appears alone.
- 1
resultstarts at0, the identity value for XOR — XOR-ing anything with0leaves it unchanged. - 2Each element in
arris XOR-ed intoresultin turn. - 3
x ^ xalways evaluates to0for any valuex, so every element that appears twice cancels itself out somewhere in the sequence. - 4Only the element with no matching pair survives all the cancellations, ending up as the final value of
result.
[7, 3, 5, 3, 7], the two 7s cancel each other out, the two 3s cancel each other out, and only 5 is left in result.Key Point: XOR is both commutative and associative, so the order the elements are XOR-ed in doesn't matter — every matching pair cancels regardless of where it sits in the array.
Why: A single pass XORs every element into one running result, with no extra data structure needed at all.
Key Concepts
Approach 2: HashMap Frequency
import java.util.HashMap;
import java.util.Map;
public class FindUniqueElementMap {
public static void main(String[] args) {
int[] arr = {7, 3, 5, 3, 7};
Map<Integer, Integer> freq = new HashMap<>();
for (int num : arr) {
// getOrDefault(num, 0) reads the current count, or 0 if unseen, then increments it
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
if (entry.getValue() == 1) { // appeared exactly once
System.out.println("Unique element: " + entry.getKey());
break;
}
}
}
}
Output
Core Logic
Counting every element's frequency and reporting the one with a count of exactly one works even without the 'everyone else appears exactly twice' guarantee the XOR trick relies on.
- 1A
HashMap<Integer, Integer>namedfreqtracks each element's count, built the same way as any other frequency map. - 2
freq.getOrDefault(num, 0)reads the current count, or0if unseen, thenfreq.put(num, ...)stores the incremented count. - 3A second loop walks
entrySet(), looking for the one entry whose count equals1. - 4That entry's key is the unique element, printed as soon as it's found.
[7, 3, 5, 3, 7], the frequency map ends up as 7: 2, 3: 2, 5: 1, so the entry with count 1 — 5 — is reported.Key Point: This is more general than the XOR trick — it still works if elements appear three or more times, as long as exactly one element has a count different from the rest.
Why: Building the frequency map visits every element once and holds one entry per distinct value, up to n in the worst case.