Find the XOR of Numbers Which Appear Twice
EASYDescription
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] <= 50- Each number in
numsappears either once or twice.
Approaches
Checkout 3 different approaches to solve Find the XOR of Numbers Which Appear Twice. Click on different approaches to view the approach and algorithm in detail.
Single Pass with a Set
This approach improves upon the brute-force method by using a HashSet to keep track of numbers encountered so far. This allows us to find duplicates in a single pass through the array, leading to a linear time complexity.
Algorithm
- Initialize a result variable
xorResultto 0. - Initialize a
HashSetcalledseento store numbers encountered so far. - Iterate through each number
numin thenumsarray. - For each
num, attempt to add it to theseenset. - The
addmethod of aHashSetreturnsfalseif the element is already present. - If
seen.add(num)returnsfalse, it signifies a duplicate. XOR thisnumwithxorResult. - If it returns
true, it's the first occurrence of the number, so we do nothing. - After the loop, return
xorResult.
We initialize a result variable xorResult to 0 and a HashSet called seen. We then iterate through each number num in the nums array. For each num, we check if it's already in the seen set. A neat trick is to use the boolean return value of seen.add(num). If the number is already present, add returns false, and we know it's a duplicate. In this case, we XOR the number with our xorResult. If the number is not present, add returns true, and we simply move on. After iterating through all the numbers, xorResult will contain the final XOR sum of all duplicate numbers.
import java.util.HashSet;
import java.util.Set;
class Solution {
public int duplicateNumbersXOR(int[] nums) {
int xorResult = 0;
Set<Integer> seen = new HashSet<>();
for (int num : nums) {
// If the number is already in the set, it's a duplicate.
if (!seen.add(num)) {
xorResult ^= num;
}
}
return xorResult;
}
}
Complexity Analysis
Pros and Cons
- Efficient O(n) time complexity.
- Conceptually simple and easy to implement.
- Requires extra space for the set, which can be O(n) in the worst case (when all elements are unique).
Code Solutions
Checking out 3 solutions in different languages for Find the XOR of Numbers Which Appear Twice. Click on different languages to view the code.
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;
}
}
Video Solution
Watch the video walkthrough for Find the XOR of Numbers Which Appear Twice
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.