内卷地狱

2341. Maximum Number of Pairs in Array — Daily Problem

Edit Me

Problem

2341. Maximum Number of Pairs in Array

Approach

My Approach

Not sure if it was seeing the word "Easy" that made me go for the optimal solution right away. Actually, ylb's hash table approach is still faster here. Sort the list and check pairs by scanning adjacent equal elements.

Hash Table Approach

After counting with Counter, use a += v // 2 and b += v % 2. For each number x with count v:

  • If v >= 1, we can form v // 2 pairs from the x values in the array.
  • Accumulate this count into variable a.

Code

class Solution:
    def numberOfPairs(self, nums: List[int]) -> List[int]:
        nums.sort()
        ans = [0, len(nums)]
        for index in range(1, len(nums)):
            if nums[index - 1] == nums[index]:
                ans[0] += 1
                ans[1] -= 2
                nums[index - 1] = nums[index] = -1
        return ans
class Solution:
    def numberOfPairs(self, nums: List[int]) -> List[int]:
        x = Counter(nums)
        a = 0
        b = 0
        for k,v in x.items():
            a+=v//2
            b+=v%2
        return [a,b]

贡献者


这篇文章有帮助吗?

最近更新

Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0CCBYNCSA