56-Number of occurrences of a number in an array-python

Question: Except for two numbers in an integer array, other numbers appear twice. Please find the two numbers that only appear once. The time complexity is O(n), and the space complexity is O(1). The code part of this question is a simplified version, only one number appears once, the others are twice.

def find_appear(arrys):
    res = 0
    for arry in arrys:
        res^=arry
    return res

  Note: Use exclusive OR operation. Use the characteristics of XOR: A number is XORed with 0 or it is itself; a number XORed with itself is 0. There are two problems that occur once. You need to divide the original array into two parts and handle them separately. Each part has only one number that appears only once.

 

Guess you like

Origin blog.csdn.net/wh672843916/article/details/105503759