Problem:list of elements, one is unique, others always in pairs, find unique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique(arr): | |
""" find unique element in array | |
1. only one is unique | |
2. all the others are in pairs | |
>>> unique([1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]) | |
6 | |
A XOR A = 0 | |
0 XOR A = A | |
""" | |
x = 0 | |
for i in arr: | |
x ^= i | |
return x | |
Brak komentarzy:
Prześlij komentarz