Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created March 17, 2025 18:26
Show Gist options
  • Save Ifihan/96ce56f698cd28088b8acb10bb3b7d6f to your computer and use it in GitHub Desktop.
Save Ifihan/96ce56f698cd28088b8acb10bb3b7d6f to your computer and use it in GitHub Desktop.
Divide Array Into Equal Pairs

Question

Approach

I use a frequency counter to count the occurrences of each number in nums. If every number appears an even number of times, then nums can be divided into valid pairs. Else it's not possible to divide the array into required pairs.

Implementation

class Solution:
    def divideArray(self, nums: List[int]) -> bool:
        count = Counter(nums)
        return all(v % 2 == 0 for v in count.values())

Complexities

  • Time: O(n)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment