mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
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>
This commit is contained in:
parent
eaa87bd791
commit
cfabd91a8b
21
bit_manipulation/missing_number.py
Normal file
21
bit_manipulation/missing_number.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
def find_missing_number(nums: list[int]) -> int:
|
||||
"""
|
||||
Finds the missing number in a list of consecutive integers.
|
||||
|
||||
Args:
|
||||
nums: A list of integers.
|
||||
|
||||
Returns:
|
||||
The missing number.
|
||||
|
||||
Example:
|
||||
>>> find_missing_number([0, 1, 3, 4])
|
||||
2
|
||||
"""
|
||||
n = len(nums)
|
||||
missing_number = n
|
||||
|
||||
for i in range(n):
|
||||
missing_number ^= i ^ nums[i]
|
||||
|
||||
return missing_number
|
Loading…
Reference in New Issue
Block a user