나는 이렇게 학습한다/Algorithm & SQL
Find the stray number
daco2020
2022. 8. 19. 21:55
You are given an odd-length array of integers, in which all of them are the same, except for one single number.
Complete the method which accepts such an array, and returns that single different number.
The input array will always be valid! (odd-length >= 3)
Examples
[1, 1, 2] ==> 2
[17, 17, 3, 17, 17, 17, 17] ==> 3
Solution:
def stray(arr):
import collections
return [k for k, v in collections.Counter(arr).items() if v == 1].pop()
Other Solution:
def stray(arr):
return min(arr, key=arr.count)