Divide Array Into Equal Pairs

Easy

Description

You are given an integer array nums consisting of 2 * n integers. Divide nums into n pairs such that each element belongs to exactly one pair. Return true if nums can be divided into pairs where each pair contains two equal integers.

Examples

Input:nums = [3,2,3,2,2,2]
Output:true
Explanation:

Pairs: (2,2), (2,2), (3,3).

Input:nums = [3, 2, 3, 2, 2, 2]
Output:true
Explanation:

Can be divided into pairs: (2,2), (2,2), (3,3).

Input:nums = [1,1,2,2,3,4]
Output:false
Explanation:

Pairs (1,1) and (2,2) can be formed, but 3 and 4 remain and cannot form a pair of equal integers. Since exactly n pairs of equal elements are needed, this array cannot be divided as required.

Constraints

  • nums.length == 2 * n
  • 1 ≤ n ≤ 500

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!