【每日一题0716】寻找数字

已知一个整数列表,请编写一个函数,找出唯一一个出现奇数次数的那个数字。

提示:

  1. 总是只有一个整数出现奇数次。

题目难度:中等
题目来源:codewar

def find_it(nums:list):
    pass

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):
    result = []
    for i in nums:
        if i not in result:
            result.append(i)
        else:
            result.remove(i)
    if len(result) != 1:
        return False
    else:
        return result[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

使用count函数 简单方便

def find_it(nums:list):
    for i in nums:
        res=nums.count(i)
        if res%2==1:
            return i

print(find_it([20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]))
print(find_it([1,1,2,-2,5,2,4,4,-1,-2,5]))
print(find_it([10]))

class Solution:
    @classmethod
    方法一
    def find_it(cls, nums: List[int]) -> int:
        """
        1、定义字典,遍历列表,统计元素在列表中出现次数并保存到字典里.
        2、遍历字典,判断次数为奇数的返回
        :param nums
        :return:int
        """
        item_count = {}
        for count in nums:
            item_count[count] = nums.count(count)
        for i, j in item_count.items():
            if j % 2 == 0:
                continue
            return i

    @classmethod
    方法二
    def find_its(cls, nums: List[int]) -> int:
        """
        1、通过numpy将list转换成sclar list
        2、统计出元素出现的次数放到字典
        3、遍历字典,判断次数为奇数的返回
        :param nums
        :return: int
        """
        arr = np.array(nums)
        new_arr = np.unique(arr)
        item_count = {}
        for i in new_arr:
            flag = (i == arr)
            arr_new = arr[flag]
            item_count[i] = arr_new.size
        for key, value in item_count.items():
            if value % 2 == 0:
                continue
            return key


assert_that(Solution.find_it(
    [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]), equal_to(5))
assert_that(Solution.find_its(
    [1, 1, 2, -2, 5, 2, 4, 4, -1, -2, 5]), equal_to(-1))
assert_that(Solution.find_its([10]), equal_to(10))
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
def find_it(li):
    """  0716 循环奇数个的那个数字 """
    # li = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]
    for i in li:
        num = li.count(i)
        if num%2 == 1:
            return i
def find_it(nums: list):
    res = {}
    for i in nums:
        if i in res.keys():
            res[i] += 1
        else:
            res[i] = 1
    for k, v in res.items():
        if v % 2 != 0:
            return k
        

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
关闭