Skip to content

Commit

Permalink
Add missing number algorithm (TheAlgorithms#9203)
Browse files Browse the repository at this point in the history
* Added missing_number algorithm using bit manipulation

* Update bit_manipulation/missing_number.py

---------

Co-authored-by: Christian Clauss <[email protected]>
  • Loading branch information
shreyabhalgat and cclauss authored Oct 1, 2023
1 parent eaa87bd commit cfabd91
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions bit_manipulation/missing_number.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cfabd91

Please sign in to comment.