Check whether an array can be split into pairs of equal values

Quick Overview

Decide whether an even-length integer array can be divided into pairs where both elements of each pair are equal and every element is used once. It tests frequency counting, a short argument for why the counting test is correct, and linear-time complexity analysis.

Check whether an array can be split into pairs of equal values

Company: ServiceNow

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `nums` consisting of `2 * n` integers. You need to divide `nums` into `n` pairs such that: - Each element belongs to exactly one pair. - The elements present in a pair are equal. Return `True` if `nums` can be divided into `n` pairs, otherwise return `False`. ### Function Signature ```python def divide_array(nums: list[int]) -> bool: ``` ### Rules - Pairs are made from array positions, so a value that appears four times can form two pairs. ### Constraints - `len(nums) == 2 * n` - `1 <= n <= 500` - `1 <= nums[i] <= 500` ### Examples ```text Input: nums = [3,2,3,2,2,2] Output: True ``` There are 6 elements, so 3 pairs are needed. The pairs `(2, 2)`, `(3, 3)` and `(2, 2)` use every element once. ```text Input: nums = [1,2,3,4] Output: False ``` There is no way to divide the array into pairs of equal elements. For example, 1 appears only once and has no partner. ```text Input: nums = [7,7,7,8,8,8] Output: False ``` The length is even, but each value appears three times. After pairing two 7s and two 8s, one 7 and one 8 are left over, and they are not equal.

Overview: Decide whether an even-length integer array can be divided into pairs where both elements of each pair are equal and every element is used once. It tests frequency counting, a short argument for why the counting test is correct, and linear-time complexity analysis.

ServiceNow logo
ServiceNow
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums consisting of 2 * n integers.

You need to divide nums into n pairs such that:

  • Each element belongs to exactly one pair.
  • The elements present in a pair are equal.

Return True if nums can be divided into n pairs, otherwise return False.

Function Signature

def divide_array(nums: list[int]) -> bool:

Rules

  • Pairs are made from array positions, so a value that appears four times can form two pairs.

Constraints

  • len(nums) == 2 * n
  • 1 <= n <= 500
  • 1 <= nums[i] <= 500

Examples

Input: nums = [3,2,3,2,2,2]
Output: True

There are 6 elements, so 3 pairs are needed. The pairs (2, 2), (3, 3) and (2, 2) use every element once.

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

There is no way to divide the array into pairs of equal elements. For example, 1 appears only once and has no partner.

Input: nums = [7,7,7,8,8,8]
Output: False

The length is even, but each value appears three times. After pairing two 7s and two 8s, one 7 and one 8 are left over, and they are not equal.