From a1b41dd820caade72e3a5228756313494dc692ba Mon Sep 17 00:00:00 2001 From: "Ch.R.Bhardwaj" <131685680+RamachandraBhardwaj@users.noreply.github.com> Date: Sun, 5 Oct 2025 14:15:33 +0530 Subject: [PATCH 1/5] Add function to find two unique numbers in a list Implement a function to find two unique numbers in a list where every other number appears twice. The function raises errors for empty lists and non-integer elements. --- bit_manipulation/fin_two_unique_numbers.py | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 bit_manipulation/fin_two_unique_numbers.py diff --git a/bit_manipulation/fin_two_unique_numbers.py b/bit_manipulation/fin_two_unique_numbers.py new file mode 100644 index 000000000000..06910e506266 --- /dev/null +++ b/bit_manipulation/fin_two_unique_numbers.py @@ -0,0 +1,56 @@ +def find_two_unique_numbers(arr: list[int]) -> tuple[int,int]: + """ + Given a list of integers where every elemnt appears twice except for two numbers, + find the two numbers that appear only once. + + this method returns the tuple of two numbers that appear only once using bitwise XOR + and using the property of x & -x to isolate the rightmost bit + (different bit between the two unique numbers). + + >>> find_two_unique_numbers([1,2,3,4,1,2]) + (3, 4) + + >>> find_two_unique_numbers([4,5,6,7,4,5]) + (6, 7) + + >>> find_two_unique_numbers([10,20,30,10,20,40]) + (30, 40) + + >>> find_two_unique_numbers([2,3]) + (2, 3) + + >>> find_two_unique_numbers([]) + Traceback (most recent call last): + ... + ValueError: input list must not be empty + >>> find_two_unique_numbers([1, 'a', 1]) + Traceback (most recent call last): + ... + TypeError: all elements must be integers + """ + + if not arr: + raise ValueError("input list must not be empty") + if not all(isinstance(x,int) for x in arr): + raise TypeError("all elements must be integers") + + xor_result = 0 + for number in arr: + xor_result^=number + + righmost_bit=xor_result & -xor_result + + num1=0 + num2=0 + + for number in arr: + if number & righmost_bit: + num1^=number + else: + num2^=number + return tuple(sorted((num1,num2))) + +if __name__=="__main__": + import doctest + + doctest.testmod() From ae4103fc55fbaf59dbd25f2c1ed3aa33317f994f Mon Sep 17 00:00:00 2001 From: "Ch.R.Bhardwaj" <131685680+RamachandraBhardwaj@users.noreply.github.com> Date: Sun, 5 Oct 2025 14:25:37 +0530 Subject: [PATCH 2/5] added reference for the question --- bit_manipulation/fin_two_unique_numbers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bit_manipulation/fin_two_unique_numbers.py b/bit_manipulation/fin_two_unique_numbers.py index 06910e506266..b316482beb4c 100644 --- a/bit_manipulation/fin_two_unique_numbers.py +++ b/bit_manipulation/fin_two_unique_numbers.py @@ -1,3 +1,4 @@ +#Reference : https://www.geeksforgeeks.org/dsa/find-two-non-repeating-elements-in-an-array-of-repeating-elements def find_two_unique_numbers(arr: list[int]) -> tuple[int,int]: """ Given a list of integers where every elemnt appears twice except for two numbers, From 2ff13152586903dddca01d710f46336360ca0c75 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 08:58:45 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/fin_two_unique_numbers.py | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/bit_manipulation/fin_two_unique_numbers.py b/bit_manipulation/fin_two_unique_numbers.py index b316482beb4c..489037554654 100644 --- a/bit_manipulation/fin_two_unique_numbers.py +++ b/bit_manipulation/fin_two_unique_numbers.py @@ -1,11 +1,11 @@ -#Reference : https://www.geeksforgeeks.org/dsa/find-two-non-repeating-elements-in-an-array-of-repeating-elements -def find_two_unique_numbers(arr: list[int]) -> tuple[int,int]: +# Reference : https://www.geeksforgeeks.org/dsa/find-two-non-repeating-elements-in-an-array-of-repeating-elements +def find_two_unique_numbers(arr: list[int]) -> tuple[int, int]: """ Given a list of integers where every elemnt appears twice except for two numbers, find the two numbers that appear only once. this method returns the tuple of two numbers that appear only once using bitwise XOR - and using the property of x & -x to isolate the rightmost bit + and using the property of x & -x to isolate the rightmost bit (different bit between the two unique numbers). >>> find_two_unique_numbers([1,2,3,4,1,2]) @@ -32,26 +32,27 @@ def find_two_unique_numbers(arr: list[int]) -> tuple[int,int]: if not arr: raise ValueError("input list must not be empty") - if not all(isinstance(x,int) for x in arr): + if not all(isinstance(x, int) for x in arr): raise TypeError("all elements must be integers") - + xor_result = 0 for number in arr: - xor_result^=number + xor_result ^= number - righmost_bit=xor_result & -xor_result + righmost_bit = xor_result & -xor_result - num1=0 - num2=0 + num1 = 0 + num2 = 0 for number in arr: if number & righmost_bit: - num1^=number + num1 ^= number else: - num2^=number - return tuple(sorted((num1,num2))) + num2 ^= number + return tuple(sorted((num1, num2))) + -if __name__=="__main__": +if __name__ == "__main__": import doctest doctest.testmod() From c68a6af8d2cbeba69d510301c5ff9423d79b4375 Mon Sep 17 00:00:00 2001 From: "Ch.R.Bhardwaj" <131685680+RamachandraBhardwaj@users.noreply.github.com> Date: Sun, 5 Oct 2025 14:38:04 +0530 Subject: [PATCH 4/5] fixes done to pass the checks --- bit_manipulation/fin_two_unique_numbers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/fin_two_unique_numbers.py b/bit_manipulation/fin_two_unique_numbers.py index 489037554654..96592e7b9688 100644 --- a/bit_manipulation/fin_two_unique_numbers.py +++ b/bit_manipulation/fin_two_unique_numbers.py @@ -1,7 +1,7 @@ # Reference : https://www.geeksforgeeks.org/dsa/find-two-non-repeating-elements-in-an-array-of-repeating-elements def find_two_unique_numbers(arr: list[int]) -> tuple[int, int]: """ - Given a list of integers where every elemnt appears twice except for two numbers, + Given a list of integers where every element appears twice except for two numbers, find the two numbers that appear only once. this method returns the tuple of two numbers that appear only once using bitwise XOR @@ -49,7 +49,8 @@ def find_two_unique_numbers(arr: list[int]) -> tuple[int, int]: num1 ^= number else: num2 ^= number - return tuple(sorted((num1, num2))) + a,b=sorted((num1,num2)) + return a,b if __name__ == "__main__": From 0d5a797578b91684e97f6355383632b4bfa73d44 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 09:08:23 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/fin_two_unique_numbers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/fin_two_unique_numbers.py b/bit_manipulation/fin_two_unique_numbers.py index 96592e7b9688..0fcd7e23f581 100644 --- a/bit_manipulation/fin_two_unique_numbers.py +++ b/bit_manipulation/fin_two_unique_numbers.py @@ -49,8 +49,8 @@ def find_two_unique_numbers(arr: list[int]) -> tuple[int, int]: num1 ^= number else: num2 ^= number - a,b=sorted((num1,num2)) - return a,b + a, b = sorted((num1, num2)) + return a, b if __name__ == "__main__":