Skip to content

Commit 2fec97b

Browse files
committed
Algorithm solving problem "136. Single Number" from Leetcode (https://leetcode.com/problems/single-number/)
1 parent 0e2e6ab commit 2fec97b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: bit_manipulation/single_number.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Given a non-empty array of integers nums, every element
3+
appears twice except for one. Find that single one.
4+
5+
You must implement a solution with a linear runtime complexity
6+
and use only constant extra space.
7+
8+
Reference: https://leetcode.com/problems/single-number/
9+
"""
10+
11+
12+
def single_number(nums: list) -> int:
13+
"""
14+
:param nums: A non-empty array of any integers nums,
15+
every element appears twice except for one.
16+
:return: element that appears only one time
17+
"""
18+
result = 0
19+
20+
for el in nums:
21+
result ^= el
22+
23+
return result

0 commit comments

Comments
 (0)