找出数组中不重复的数-python

1.数组中不重复的数只有一个,初始值为0,然后直接遍历数组,让每个值与初始值进行异或,得出的最终值就是要找的结果:

# Your Code Here
    single = 0
    for v in data:
        single=single^v
    return single
# End Your Code

2.数组中不重复的数大于一个,则可以遍历一次数组,使用一个map记录数与数出现的次数;接着,再遍历一次map,找出次数为1的数,即为我们要找的数:

# Your Code Here
    single = 0
    Map = {}
    for i in range(len(data)):
        if data[i] in Map:
            Map[data[i]] += 1
        else:
            Map[data[i]] = 1
    for key in Map:
        if Map[key] == 1:
            single = key
            break
    return single
# End Your Code

 

你可能感兴趣的:(python)