Divide Array Into Equal Pairs

You are given an integer array nums of 2 * n integers. Decide whether nums can be divided into n pairs so that every element belongs to exactly one pair and the two elements of each pair are equal. Return true if it can, otherwise false.

30:00

Divide Array Into Equal Pairs
easy
Free written solution
Topics
Companies

You are given an integer array nums of 2 * n integers. Decide whether nums can be divided into n pairs so that every element belongs to exactly one pair and the two elements of each pair are equal. Return true if it can, otherwise false.

Example 1:
Input: {"nums":[3,2,3,2,2,2]}
Output: true
Constraints:
  • nums.length==2⋅nnums.length == 2 \cdot n

  • 1≤n≤5001 \leq n \leq 500

  • 1≤nums[i]≤5001 \leq nums[i] \leq 500

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,2,3,2,2,2]}

Give each value a parity switch, all off. A switch is on while one copy of that value is still waiting for its partner.

nums, one at a time

0 / 6 read

3
2
3
2
2
2

odd[x]: on while a copy of x waits for its partner

unpaired = 0

2

no pair

3

no pair

● on = odd count so far

● off = every copy paired

● still on at the end = cannot pair

Variables
VariableValue
unpaired0
DepthFunction Call
Stack empty
0/7