313. How Many Equal Pairs
Print how many pairs of positions (i, j) with i < j hold equal values.
1 2 1 2 1 -> 4
The three ones give three pairs and the two twos give one, so four in total.
Counting the pairs directly is quadratic. Count each value instead: a value
appearing c times contributes c * (c - 1) / 2 pairs, and adding those up
over the distinct values is linear.
Constraints - `1 ≤ n ≤ 200000` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains an integer n.
The second line contains n integers.
Output
Print one integer, the number of index pairs holding equal values.
Hints
Four rungs, in order. The last two open once you have submitted an attempt — a wrong one counts.