Find the XOR of Numbers Which Appear Twice - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,2]
Output: 2
💡 Note: Only the number 2 appears twice in the array, so we return 2
Example 2 — Multiple Duplicates
$ Input: nums = [1,2,1,3]
Output: 1
💡 Note: Only the number 1 appears twice, so we return 1
Example 3 — No Duplicates
$ Input: nums = [1,2,3]
Output: 0
💡 Note: No number appears twice, so we return 0

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 50

Visualization

Tap to expand
Find XOR of Numbers Appearing TwiceInput: [1, 2, 3, 2]1232oncetwiceoncetwiceOnly number 2 appears twiceXOR of duplicates: 2Result: 2
Understanding the Visualization
1
Input Array
Array with numbers appearing once or twice: [1,2,3,2]
2
Find Duplicates
Identify which numbers appear exactly twice: 2
3
XOR Result
XOR all duplicate numbers together: 2
Key Takeaway
🎯 Key Insight: Use hash set to track seen numbers and XOR duplicates as they're found
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~10 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen