Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an array nums, where each number in the array appears either once or twice.
Return the bitwise XOR of all the numbers that appear twice in the array, or 0 if no number appears twice.
Example 1:
Input: nums = [1,2,1,3]
Output: 1
Explanation:
The only number that appears twice in nums is 1.
Example 2:
Input: nums = [1,2,3]
Output: 0
Explanation:
No number appears twice in nums.
Example 3:
Input: nums = [1,2,2,1]
Output: 3
Explanation:
Numbers 1 and 2 appeared twice. 1 XOR 2 == 3.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 50nums appears either once or twice.Problem summary: You are given an array nums, where each number in the array appears either once or twice. Return the bitwise XOR of all the numbers that appear twice in the array, or 0 if no number appears twice.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
[1,2,1,3]
[1,2,3]
[1,2,2,1]
single-number)single-number-ii)single-number-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3158: Find the XOR of Numbers Which Appear Twice
class Solution {
public int duplicateNumbersXOR(int[] nums) {
int[] cnt = new int[51];
int ans = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans ^= x;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3158: Find the XOR of Numbers Which Appear Twice
func duplicateNumbersXOR(nums []int) (ans int) {
cnt := [51]int{}
for _, x := range nums {
cnt[x]++
if cnt[x] == 2 {
ans ^= x
}
}
return
}
# Accepted solution for LeetCode #3158: Find the XOR of Numbers Which Appear Twice
class Solution:
def duplicateNumbersXOR(self, nums: List[int]) -> int:
cnt = Counter(nums)
return reduce(xor, [x for x, v in cnt.items() if v == 2], 0)
// Accepted solution for LeetCode #3158: Find the XOR of Numbers Which Appear Twice
/**
* [3158] Find the XOR of Numbers Which Appear Twice
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn duplicate_numbers_xor(nums: Vec<i32>) -> i32 {
use std::collections::HashSet;
let mut set = HashSet::with_capacity(nums.len());
let mut result = 0;
for num in nums {
set.insert(num);
result ^= num;
}
for num in set {
result ^= num;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3158() {
assert_eq!(1, Solution::duplicate_numbers_xor(vec![1, 2, 1, 3]));
assert_eq!(0, Solution::duplicate_numbers_xor(vec![1, 2, 3]));
assert_eq!(3, Solution::duplicate_numbers_xor(vec![1, 2, 2, 1]));
}
}
// Accepted solution for LeetCode #3158: Find the XOR of Numbers Which Appear Twice
function duplicateNumbersXOR(nums: number[]): number {
const cnt: number[] = Array(51).fill(0);
let ans: number = 0;
for (const x of nums) {
if (++cnt[x] === 2) {
ans ^= x;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.