From cfabd91a8ba83bbe23d2790494e2450118044fcc Mon Sep 17 00:00:00 2001 From: Shreya Bhalgat <85868386+shreyabhalgat@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:58:20 +0530 Subject: [PATCH] Add missing number algorithm (#9203) * Added missing_number algorithm using bit manipulation * Update bit_manipulation/missing_number.py --------- Co-authored-by: Christian Clauss --- bit_manipulation/missing_number.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 bit_manipulation/missing_number.py diff --git a/bit_manipulation/missing_number.py b/bit_manipulation/missing_number.py new file mode 100644 index 000000000..92502a778 --- /dev/null +++ b/bit_manipulation/missing_number.py @@ -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