From a50dd5b8490e186257f27a6b6c76fa542d6be91b Mon Sep 17 00:00:00 2001 From: Irfan Ansari <71096605+IRFANSARI@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:33:00 +0530 Subject: [PATCH 1/5] Add files via upload --- ..._number_that_appears_only_once_in_array.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bit_manipulation/find_number_that_appears_only_once_in_array.py diff --git a/bit_manipulation/find_number_that_appears_only_once_in_array.py b/bit_manipulation/find_number_that_appears_only_once_in_array.py new file mode 100644 index 000000000000..0c95d2d283d7 --- /dev/null +++ b/bit_manipulation/find_number_that_appears_only_once_in_array.py @@ -0,0 +1,44 @@ +''' +This problem comes from LeetCode 136: Single Number + +Problem Statement: You are given an list of number where each number appears twice in the list but there is a single number that appears once only. Find that single number. + +Intuition: +- Hence we know that a ^ a = 0 (^ is XOR binary operator) +- We can XOR all numbers, then we will get the only single number +- Example: let's take nums = [1, 2, 4, 1, 4, 3, 2] + - We can see all numbers appears twice except 3 + - If we take XOR of all element, we will get 3, let me demonstrate - + - 1 ^ 2 ^ 4 ^ 1 ^ 4 ^ 3 ^ 2 + - Since we know a ^ a = 0, we can cancel them out - + - 1 ^ 1 ^ 2 ^ 2 ^ 4 ^ 4 ^ 3 + - 0 ^ 0 ^ 0 ^ 3 + - We also know a ^ 0 = a, then we will get our answer as 3 + +Complexities: +- Time: O(n) +- Space: O(1) +''' + +def find_single_number(nums): + ''' + LeetCode 136: Single Number + https://leetcode.com/problems/single-number/description/ + + >>> find_single_number([1, 4, 1, 7, 9, 2, 9, 7, 2]) + 4 + >>> find_single_number([1, 2, 4, 1, 4, 3, 2]) + 3 + >>> find_single_number([4, 1, 2, 1, 2]) + 4 + ''' + single_num = 0 + + for i in nums: + single_num = single_num ^ i; + + return single_num + +if __name__ == "__main__": + import doctest + doctest.testmod() From 036b554db6bf35b77a1026c13f021feaf7a02968 Mon Sep 17 00:00:00 2001 From: Irfan Ansari <71096605+IRFANSARI@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:39:46 +0530 Subject: [PATCH 2/5] Update find_number_that_appears_only_once_in_array.py --- ..._number_that_appears_only_once_in_array.py | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/bit_manipulation/find_number_that_appears_only_once_in_array.py b/bit_manipulation/find_number_that_appears_only_once_in_array.py index 0c95d2d283d7..5b33f190fcf5 100644 --- a/bit_manipulation/find_number_that_appears_only_once_in_array.py +++ b/bit_manipulation/find_number_that_appears_only_once_in_array.py @@ -1,25 +1,3 @@ -''' -This problem comes from LeetCode 136: Single Number - -Problem Statement: You are given an list of number where each number appears twice in the list but there is a single number that appears once only. Find that single number. - -Intuition: -- Hence we know that a ^ a = 0 (^ is XOR binary operator) -- We can XOR all numbers, then we will get the only single number -- Example: let's take nums = [1, 2, 4, 1, 4, 3, 2] - - We can see all numbers appears twice except 3 - - If we take XOR of all element, we will get 3, let me demonstrate - - - 1 ^ 2 ^ 4 ^ 1 ^ 4 ^ 3 ^ 2 - - Since we know a ^ a = 0, we can cancel them out - - - 1 ^ 1 ^ 2 ^ 2 ^ 4 ^ 4 ^ 3 - - 0 ^ 0 ^ 0 ^ 3 - - We also know a ^ 0 = a, then we will get our answer as 3 - -Complexities: -- Time: O(n) -- Space: O(1) -''' - def find_single_number(nums): ''' LeetCode 136: Single Number From 11bc9e328fada4f9d39f7bac8905757bfcee8f08 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:12:40 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../find_number_that_appears_only_once_in_array.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bit_manipulation/find_number_that_appears_only_once_in_array.py b/bit_manipulation/find_number_that_appears_only_once_in_array.py index 5b33f190fcf5..ab6be9976dbb 100644 --- a/bit_manipulation/find_number_that_appears_only_once_in_array.py +++ b/bit_manipulation/find_number_that_appears_only_once_in_array.py @@ -1,5 +1,5 @@ def find_single_number(nums): - ''' + """ LeetCode 136: Single Number https://leetcode.com/problems/single-number/description/ @@ -9,14 +9,16 @@ def find_single_number(nums): 3 >>> find_single_number([4, 1, 2, 1, 2]) 4 - ''' + """ single_num = 0 - + for i in nums: - single_num = single_num ^ i; - + single_num = single_num ^ i + return single_num + if __name__ == "__main__": import doctest + doctest.testmod() From a99facf5080a40f626115ff0dc08a2b6f086bdaa Mon Sep 17 00:00:00 2001 From: Irfan Ansari <71096605+IRFANSARI@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:52:55 +0530 Subject: [PATCH 4/5] Update find_number_that_appears_only_once_in_array.py --- .../find_number_that_appears_only_once_in_array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/find_number_that_appears_only_once_in_array.py b/bit_manipulation/find_number_that_appears_only_once_in_array.py index ab6be9976dbb..d2c12cde7671 100644 --- a/bit_manipulation/find_number_that_appears_only_once_in_array.py +++ b/bit_manipulation/find_number_that_appears_only_once_in_array.py @@ -1,7 +1,7 @@ -def find_single_number(nums): +def find_single_number(nums: List[int]) -> int: """ LeetCode 136: Single Number - https://leetcode.com/problems/single-number/description/ + https://leetcode.com/problems/single-number/ >>> find_single_number([1, 4, 1, 7, 9, 2, 9, 7, 2]) 4 From f1f65b4e34b9cc4268b7a429ae8e57ac48695afa Mon Sep 17 00:00:00 2001 From: Irfan Ansari <71096605+IRFANSARI@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:58:37 +0530 Subject: [PATCH 5/5] Update find_number_that_appears_only_once_in_array.py --- bit_manipulation/find_number_that_appears_only_once_in_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/find_number_that_appears_only_once_in_array.py b/bit_manipulation/find_number_that_appears_only_once_in_array.py index d2c12cde7671..edb4aa92d2cc 100644 --- a/bit_manipulation/find_number_that_appears_only_once_in_array.py +++ b/bit_manipulation/find_number_that_appears_only_once_in_array.py @@ -1,4 +1,4 @@ -def find_single_number(nums: List[int]) -> int: +def find_single_number(nums: list[int]) -> int: """ LeetCode 136: Single Number https://leetcode.com/problems/single-number/