Single Number: the idea to remember.
In an integer array every value occurs twice except one; return the unique value.
The core insight
XOR every value together so paired values cancel and the unique value remains.
Why it works
XOR is associative and commutative, with x XOR x equal to zero and x XOR zero equal to x.
The mistake to avoid
This invariant depends on pairs; it does not directly handle values occurring three times.
Commented Java solution
Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.
// Java reference adapted from neetcode-gh/leetcode (MIT).
// XOR every value together so paired values cancel and the unique value remains.
public int singleNumber(int[] nums) {
int ans = nums[0];
for (int i = 1; i < nums.length; i++) ans ^= nums[i];
return ans;
}Java reference adapted from neetcode-gh/leetcode · MIT license
Make the next connection
Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.