def find_it(nums:list):
for i in nums:
n = nums.count(i)
if n%2 != 0:
return i
assert find_it([20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]) == 5
assert find_it([1,1,2,-2,5,2,4,4,-1,-2,5]) == -1
assert find_it([10]) == 10
import pytest
@pytest.mark.parametrize("data,expect",[([20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5],5),
([1,1,2,-2,5,2,4,4,-1,-2,5],-1),
([10],10)])
def test_find_it(data,expect):
list1=list(set(data))
dict1={}
for i in list1:
dict1[i] = Counter(data)[i]
for i in dict1.values():
if i%2 !=0 :
assert {value:key for key,value in dict1.items()}[i] == expect
def find_it(nums: list):
frequency_list = []
for n in nums:
if n in [i[0] for i in frequency_list if len(frequency_list) > 0]:
index = 0
for f in frequency_list:
if f[0] == n:
frequency_list[index][1] += 1
index += 1
else:
frequency_list.append([n, 1])
for f in frequency_list:
if f[1] % 2 == 1:
print(f[0])
return f[0]
assert find_it([20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]) == 5
assert find_it([1, 1, 2, -2, 5, 2, 4, 4, -1, -2, 5]) == -1
assert find_it([10]) == 10
def find_it(nums:list):
_dict = {}
for i in range(len(nums)):
if nums[i] in _dict: _dict[nums[i]].append(i)
else: _dict[nums[i]]=[i]
for key in _dict.keys():
if len(_dict[key])%2==1: return key
return None
第二种:比较拉的:
def find_it(nums:list):
s_list = sorted(nums)
start_index,end_index,start_w = -1,-1,None
for i in range(len(s_list)):
if start_w != s_list[i]:
if end_index != -1 and (end_index-start_index)%2==0:
return start_w
else:
start_w = s_list[i]
start_index = i
end_index = i
if (end_index-start_index)%2==0: return start_w
else: return None
def find_it(nums:list):
dict_nums={}
for i in range(len(nums)):
if nums[i] not in dict_nums.keys():
dict_nums[nums[i]]=1
else:
dict_nums[nums[i]]+=1
for j in dict_nums.items():
if j[1]%2 !=0:
return j[0]
assert find_it([20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]) == 5
assert find_it([1,1,2,-2,5,2,4,4,-1,-2,5]) == -1
assert find_it([10]) == 10