Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added missing_number algorithm using bit manipulation
  • Loading branch information
shreyabhalgat committed Oct 1, 2023
commit f94b52fd0afe1fbb4e1d60e7ea10877a073715f3
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):
"""
Finds the missing number in a list of consecutive integers.

Args:
nums (List[int]): A list of integers.

Returns:
int: The missing number.
Copy link
Member

@cclauss cclauss Oct 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy can test data types in the function signature, but not in the comments.

Do not repeat the datatypes in both places because readers will be confused if one is changed and the other is not.

Suggested change
def find_missing_number(nums):
"""
Finds the missing number in a list of consecutive integers.
Args:
nums (List[int]): A list of integers.
Returns:
int: The missing number.
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