mang_dev
맹꽁거리는 개발자
mang_dev
전체 방문자
오늘
어제
  • 분류 전체보기 (185)
    • Frontend (2)
      • Next.js (1)
    • Backend (3)
      • GraphQL (2)
    • Book (1)
      • 기타 (1)
    • Old (177)
      • 알고리즘 퍼즐 (1)
      • 백준 (131)
      • 프로그래머스 (0)
      • Codility (15)
      • LeetCode (7)
      • Codewars (1)
      • Codeforces (0)
      • Django (6)
      • React (2)
      • Naver Map Api (3)
      • Web UI (4)
      • Introduction to Cloud (2)
hELLO · Designed By 정상우.
mang_dev

맹꽁거리는 개발자

6kyu - Find the unique number // Python
Old/Codewars

6kyu - Find the unique number // Python

2021. 1. 25. 22:00

문제

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
저작자표시 (새창열림)
    mang_dev
    mang_dev

    티스토리툴바