Strony

niedziela, 21 lutego 2021

 Problem:list of elements, one is unique, others always in pairs, find unique



 

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
view raw unique hosted with ❤ by GitHub

Brak komentarzy:

Prześlij komentarz