You are given an array of n integers. Count the number of index pairs (i, j) with i < j and nums[i] == nums[j].
If a value occurs f times, it contributes f * (f - 1) / 2 such pairs; the total is the sum over all values.
Line 1: an integer n.
Line 2: n space-separated integers, the array.
A single integer: the number of equal-value index pairs.
Example 1
Input
5 1 2 1 3 1
Expected
3
Explanation
The value 1 appears at three positions, giving 3*2/2 = 3 pairs. No other value repeats, so the total is 3.
Example 2
Input
4 1 2 3 4
Expected
0
Explanation
All values are distinct, so there are no equal-value pairs: 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →