8000 Add 3158_find_the_xor_of_numbers_which_appear_twice · m3xw3ll/LeetCode@221fd42 · GitHub
[go: up one dir, main page]

Skip to content

Commit 221fd42

Browse files
committed
Add 3158_find_the_xor_of_numbers_which_appear_twice
1 parent 2ecb0d5 commit 221fd42

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [Find the XOR of Numbers Which Appear Twice](https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice/description/)
2+
3+
You are given an array nums, where each number in the array appears either once or twice.
4+
5+
Return the bitwise XOR of all the numbers that appear twice in the array, or 0 if no number appears twice.
6+
7+
Example 1:
8+
```
9+
Input: nums = [1,2,1,3]
10+
11+
Output: 1
12+
13+
Explanation:
14+
15+
The only number that appears twice in nums is 1.
16+
```
17+
Example 2:
18+
```
19+
Input: nums = [1,2,3]
20+
21+
Output: 0
22+
23+
Explanation:
24+
25+
No number appears twice in nums.
26+
```
27+
Example 3:
28+
```
29+
Input: nums = [1,2,2,1]
30+
31+
Output: 3
32+
33+
Explanation:
34+
35+
Numbers 1 and 2 appeared twice. 1 XOR 2 == 3.
36+
```
37+
Solution
38+
```python
39+
class Solution:
40+
def duplicateNumbersXOR(self, nums: List[int]) -> int:
41+
seen = set()
42+
dups = set()
43+
out = 0
44+
for n in nums:
45+
if n in seen:
46+
dups.add(n)
47+
else:
48+
seen.add(n)
49+
50+
for i in dups:
51+
out ^= i
52+
return out
53+
54+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def duplicate_numbers_xor(nums):
2+
seen = set()
3+
dups = set()
4+
out = 0
5+
for n in nums:
6+
if n in seen:
7+
dups.add(n)
8+
else:
9+
seen.add(n)
10+
11+
for i in dups:
12+
out ^= i
13+
return out
14+
15+
16+
print(duplicate_numbers_xor(nums=[1, 2, 2, 1]))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ To search for a specific problem please use ```STRG + F``` to search for.
758758
| 3110 | [Score of a String](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3110_score_of_a_string.md)
759759
| 3131 | [Find the Integer Added to Array I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3131_find_the_integer_added_to_array_one.md)
760760
| 3151 | [Special Array I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3151_special_array_one.md)
761+
| 3158 | [Find the XOR of Numbers Which Appear Twice](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3158_find_the_xor_of_numbers_which_appear_twice.md)
761762
| 3162 | [Find the Number of Good Pairs I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3162_find_the_number_of_good_pairs_one.md)
762763
| 3174 | [Clear Digits](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3174_clear_digits.md)
763764
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3190_find_minimum_operations_to_make_all_elements_divisible_by_three.md)

0 commit comments

Comments
 (0)
0