Find the stray number
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.C..
2022. 8. 19.
Find the odd int
Description: Given an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times. Examples [7] should return 7, because it occurs 1 time (which is odd). [0] should return 0, because it occurs 1 time (which is odd). [1,1,2] should return 2, because it occurs 1 time (which is odd). [0,1,0,1,0] should return 0, beca..
2022. 3. 30.
Python _ 리스트 요소 개수 세기(dictionary, collections)
리스트에 어떤 요소의 개수를 파악해야하는 때가 있습니다. (특히 코딩테스트에서 사용할 일이 많습니다) 그래서 오늘은 요소 개수를 세는 방법을 정리해보고자 합니다. dictionary 사용 # 요소를 세고 싶은 리스트 >>> list = [1,2,3,4,5,5,5,5,5,1,1] # 빈 딕셔너리를 생성 >>> dict = {} # 요소가 딕셔너리에 있다면 += 1, 없다면 = 1 >>> for num in list: if num in dict: # dict.get(num)로 대체가능 dict[num] += 1 else: dict[num] = 1 # {요소(키): 개수(값)} 형태의 딕셔너리 생성 >>> dict {1: 3, 2: 1, 3: 1, 4: 1, 5: 5} 기본 딕셔너리를 사용하는 베이직한 방법입..
2022. 3. 6.