Skip to content

Commit e9424d0

Browse files
solves #3158: Find the XOR of Numbers Which Appear Twice
1 parent ad6f88f commit e9424d0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@
912912
| 3142 | [Check if Grid Satisfies Conditions](https://leetcode.com/problems/check-if-grid-satisfies-conditions) | [![Java](assets/java.png)](src/CheckIfGridSatisfiesConditions.java) | |
913913
| 3146 | [Permutation Difference between Two Strings](https://leetcode.com/problems/permutation-difference-between-two-strings) | [![Java](assets/java.png)](src/PermutationDifferenceBetweenTwoStrings.java) | |
914914
| 3151 | [Special Array I](https://leetcode.com/problems/special-array-i) | [![Java](assets/java.png)](src/SpecialArrayI.java) | |
915-
| 3158 | [Find the XOR of Numbers Which Appear Twice](https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice) | | |
915+
| 3158 | [Find the XOR of Numbers Which Appear Twice](https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice) | [![Java](assets/java.png)](src/FindTheXOROfNumbersWhichAppearTwice.java) | |
916916
| 3162 | [Find the Number of Good Pairs I](https://leetcode.com/problems/find-the-number-of-good-pairs-i) | | |
917917
| 3168 | [Minimum Number of Chairs in a Waiting Room](https://leetcode.com/problems/minimum-number-of-chairs-in-a-waiting-room) | | |
918918
| 3173 | [Bitwise OR of Adjacent Elements](https://leetcode.com/problems/bitwise-or-of-adjacent-elements) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class FindTheXOROfNumbersWhichAppearTwice {
9+
public int duplicateNumbersXOR(int[] array) {
10+
final Set<Integer> numbers = new HashSet<>();
11+
int result = 0;
12+
for (int element : array) {
13+
if (numbers.contains(element)) {
14+
result ^= element;
15+
}
16+
numbers.add(element);
17+
}
18+
return result;
19+
}
20+
}

0 commit comments

Comments
 (0)