【每日一题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
def find_it(nums:list):
    return int([num for num in nums if nums.count(num) % 2 == 1 ][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):
    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 nums:
        # 如果字典中有对应key值,则value值+1
        if i in dict.keys():
            dict[i] = int(dict[i]) + 1
        else:
            # 如果字典中没有对应key值,则插入,赋值1
            dict[i] = 1
            # 便利字典items,找出values值是奇数的key值
    for i in dict.items():
        if i[1] % 2 != 0:
            return i[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):
    for x in nums:
        if nums.count(x) % 2 == 1:
            return x

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):
    for num in set(nums):
        if nums.count(num) % 2 == 1:
            return num


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_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