문제
There is an array with some numbers. All numbers are equal except for one. Try to find it!
find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0]) == 0.55
It’s guaranteed that array contains at least 3 numbers.
The tests contain some very huge arrays, so think about performance.
풀이
문제 조건에서 배열에 데이터가 많다고 했기 때문에 중복 값을 제거하는 것을 우선 순위로 생각했다.
Python에서는 배열을 Set(중복된 값이 없는 자료형)으로 만드는 Method가 있기 때문에 사용하였다.
그렇게 자료형을 바꾸고 나서, set 내의 원소를 for문을 통해 탐색하며 원래의 배열에서 count method를 사용하여 1번만 나오는 원소를 찾았다.
코드
def find_uniq(arr):
arr_set = set(arr)
n = 0
for i in arr_set:
if arr.count(i) == 1:
n = i
return n