1. Topic description
Two, code analysis
Solution 1: XOR operation
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = 0
for i in range(len(nums)):
result = result^nums[i]
return result
# solution = Solution()
# result = solution.singleNumber([4,1,2,1,2])
# print(result)
Note: The XOR of two numbers is the XOR of the corresponding binary form of the two numbers. XOR means that if it is different, it is 1, and if it is the same, it is 0, that is, the XOR of two identical numbers is 0, and the XOR of 0 and any number is the number itself.
3. Summary
The link to the official problem solution can be clicked here . The best solution to this question is the XOR operation. Of course, you can also diverge your thinking and try other methods.