O (n) реализация Python:
def find_singletons(items):
singletons = []
a, b = items[:2]
for item in items[2:]:
if a != b and b != item:
singletons.append(b)
a, b = b, item
return singletons
items = [-2, -2, 5, 5, 5, 67, 67, 72, 80, 80, 80, 80]
print(find_singletons(items))
# [72]
Другая O (n) реализация Python (с использованием счетчика):
def find_singletons2(items):
singletons = []
count = 1
last_item = items[0]
for item in items[1:]:
if last_item != item:
if count == 1:
singletons.append(last_item)
count = 1
else:
count += 1
last_item = item
return singletons
items = [-2, -2, 5, 5, 5, 67, 67, 72, 80, 80, 80, 80]
print(find_singletons(items))
# [72]