【每日一题20220516】配对个数

:mage:‍ 给定一个数字列表,请编写一个函数,从左往右,统计出所有成对出现的数字的个数。列表中每个元素只有一次统计机会。

【示例】
输入:[1, 2, 2, 20, 6, 20, 2, 6, 2]
输出:4
解释:从左往右,出现两次的数字依次有2、20、6和2。因为有4个2,所以算2对。

题目难度:简单
题目来源:CodeWars-Find all pairs

def solution(nums: list)-> iny:
    # your code here

assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3
def solution(nums: list)-> int:
    # your code here
    count = 0
    for item in set(nums):
        if nums.count(item) > 1:
            count += nums.count(item) // 2
    return count

assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3
def solution(nums: list)-> int:
    return sum([nums.count(data) // 2 for data in set(nums)])


assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3
def solution(nums: list)-> int:
    # your code here
    return sum([n for i in set(nums) if (n := (nums.count(i) // 2)) > 0])


assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3

def solution(nums: list)-> int:
a = 0
while len(nums)>0:
i = nums[0]
del nums[nums.index(i)]
if i in nums:
del nums[nums.index(i)]
a+=1
return a

assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3

def solution(nums: list):
    count = 0
    key = set(nums)
    for i in key:
        if nums.count(i) > 1:
            count += int(nums.count(i)/2)
    return count

assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3
def solution(nums: list)-> int:
    counNum=0
    nums.sort()
    for i in nums:
        if(i==temp):
            continue
        else:
            b=nums.count(i)
            temp=i
            c=int(b/2)
            if(c>=1):
                counNum=counNum+c
    return counNum

assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3
def solution(nums: list)-> int:
    num = sorted(nums)
    start =0
    pairs = 0
    while start <len(nums)-1:
        if num[start] == num[start+1]:
            pairs += 1
            start = start +2
        else:
            start += 1
    return pairs
1 个赞
from collections import Counter

def solution(nums: list)-> int:
	count=0
	res=dict(Counter(nums))
	for v in res.values():
		if v>1:
			count+=int(v/2)
	return count


assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3
def solution(nums: list)-> int:
    count=0
    for i in set(nums):
        if nums.count(i)>1:
            count+=nums.count(i)//2
    return count

assert solution([1, 2, 2, 20, 6, 20, 2, 6, 2]) == 4
assert solution([1, 2, 5, 6, 5, 2]) == 2
assert solution([0, 0, 0, 0, 0, 0, 0]) == 3