From 15658bfe12aa9f09a54dd222d0d7b1530f3e25c6 Mon Sep 17 00:00:00 2001 From: satyxm Date: Mon, 14 Oct 2024 01:01:58 +0530 Subject: [PATCH 1/2] Single Number Algo Added --- bit_manipulation/single_number.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 bit_manipulation/single_number.py diff --git a/bit_manipulation/single_number.py b/bit_manipulation/single_number.py new file mode 100644 index 000000000..e1d629dc7 --- /dev/null +++ b/bit_manipulation/single_number.py @@ -0,0 +1,30 @@ +def single_number(nums: list[int]) -> int: + """ + Find the single number in an array where every other number appears twice. + + Args: + nums: List of integers where every integer appears twice except for one. + + Returns: + The single integer that appears once. + + Example: + >>> single_number([4, 1, 2, 1, 2]) + 4 + >>> single_number([2, 2, 1]) + 1 + >>> single_number([1]) + 1 + """ + result = 0 + for num in nums: + result ^= num # Applying XOR operation + return result + + +if __name__ == "__main__": + # Test cases + print(single_number([4, 1, 2, 1, 2])) # Output: 4 + print(single_number([2, 2, 1])) # Output: 1 + print(single_number([1])) # Output: 1 + print(single_number([5, 3, 5, 7, 3])) # Output: 7 From 20070c23e55a8579d1551e5d1913673de65df7d6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Oct 2024 19:35:05 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/single_number.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/single_number.py b/bit_manipulation/single_number.py index e1d629dc7..0a59adb2d 100644 --- a/bit_manipulation/single_number.py +++ b/bit_manipulation/single_number.py @@ -25,6 +25,6 @@ def single_number(nums: list[int]) -> int: if __name__ == "__main__": # Test cases print(single_number([4, 1, 2, 1, 2])) # Output: 4 - print(single_number([2, 2, 1])) # Output: 1 - print(single_number([1])) # Output: 1 - print(single_number([5, 3, 5, 7, 3])) # Output: 7 + print(single_number([2, 2, 1])) # Output: 1 + print(single_number([1])) # Output: 1 + print(single_number([5, 3, 5, 7, 3])) # Output: 7