Skip to content

2206. Divide Array Into Equal Pairs 👍

  • Time: $O(n)$
  • Space: $O(500) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  bool divideArray(vector<int>& nums) {
    vector<int> count(501);

    for (const int num : nums)
      ++count[num];

    return ranges::all_of(count, [](int c) { return c % 2 == 0; });
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public boolean divideArray(int[] nums) {
    int[] count = new int[501];

    for (final int num : nums)
      ++count[num];

    return Arrays.stream(count).allMatch(c -> c % 2 == 0);
  }
}
1
2
3
class Solution:
  def divideArray(self, nums: list[int]) -> bool:
    return all(value % 2 == 0 for value in collections.Counter(nums).values())