Easy
Given a non-empty array of integers nums
, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,1]
Output: 1
Example 2:
Input: nums = [4,1,2,1,2]
Output: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
1 <= nums.length <= 3 * 104
-3 * 104 <= nums[i] <= 3 * 104
To solve this problem with the Solution
class, which requires linear runtime complexity and constant extra space, we can utilize the bitwise XOR operation. Here are the steps:
Solution
class with a method singleNumber
that takes a list of integers nums
as input and returns the single number that appears only once.singleNumber
method, initialize a variable result
to 0.nums
list.result
and the current element.result
will contain the single number that appears only once due to the properties of XOR operation.result
.Here’s how the Solution
class would look like in Python:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
result = 0
for num in nums:
result ^= num
return result
# Example usage:
solution = Solution()
print(solution.singleNumber([2, 2, 1])) # Output: 1
print(solution.singleNumber([4, 1, 2, 1, 2]))# Output: 4
print(solution.singleNumber([1])) # Output: 1
This solution leverages the bitwise XOR operation to find the single number that appears only once in the array, achieving linear runtime complexity and constant extra space usage.
class Solution:
def singleNumber(self, nums: List[int]) -> int:
res = 0
for num in nums:
res ^= num
return res