Skip to content

Commit cfabd91

Browse files
Add missing number algorithm (#9203)
* Added missing_number algorithm using bit manipulation * Update bit_manipulation/missing_number.py --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent eaa87bd commit cfabd91

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

bit_manipulation/missing_number.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def find_missing_number(nums: list[int]) -> int:
2+
"""
3+
Finds the missing number in a list of consecutive integers.
4+
5+
Args:
6+
nums: A list of integers.
7+
8+
Returns:
9+
The missing number.
10+
11+
Example:
12+
>>> find_missing_number([0, 1, 3, 4])
13+
2
14+
"""
15+
n = len(nums)
16+
missing_number = n
17+
18+
for i in range(n):
19+
missing_number ^= i ^ nums[i]
20+
21+
return missing_number

0 commit comments

Comments
 (0)