Count Equal-Value Index Pairs

Quick Overview

Count Equal-Value Index Pairs. Count pairs of indices(i,j) with i<j and nums[i]==nums[j]. Each pair of positions counts separately.

Count Equal-Value Index Pairs

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Count pairs of indices(i,j) with i<j and nums[i]==nums[j]. Each pair of positions counts separately. ```cpp class Solution { public: int numIdenticalPairs(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text Input: nums = [2,3,2,3,2] Output: 4 Explanation: Three2s form three pairs,and two3s form one more. ``` **Example 2:** ```text Input: nums = [5] Output: 0 Explanation: There are not two different indices. ``` ### Constraints - nums has length 1 to 100. - Every value lies in `[1,100]`. ### Contract considerations Equality of values is required; distinct positions may hold the same value. ```hint Read the boundary condition Count index pairs rather than distinct value pairs. Exclude self-pairs and reversed copies of the same pair. ``` ### Boundary reminders Equality of values is required; distinct positions may hold the same value.

Overview: Count Equal-Value Index Pairs. Count pairs of indices(i,j) with i<j and nums[i]==nums[j]. Each pair of positions counts separately.

Google logo
Google
Aug 17, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Count pairs of indices(i,j) with i<j and nums[i]==nums[j]. Each pair of positions counts separately.

class Solution {
public:
    int numIdenticalPairs(vector<int>& nums);
};

Examples

Example 1:

Input: nums = [2,3,2,3,2]
Output: 4
Explanation: Three2s form three pairs,and two3s form one more.

Example 2:

Input: nums = [5]
Output: 0
Explanation: There are not two different indices.

Constraints

  • nums has length 1 to 100.
  • Every value lies in [1,100] .

Contract considerations

Equality of values is required; distinct positions may hold the same value.

Boundary reminders

Equality of values is required; distinct positions may hold the same value.