class EqualPairCounter {
int numberOfEqualPairsLinearWithExtraMap(int[] input) {
int res = 0;
Map<Integer, Integer> vMap = new HashMap<>();
for (final int v : input) {
int count = vMap.getOrDefault(v, 0);
vMap.put(v, ++count);
}
for(final int v : vMap.keySet()) {
if (vMap.get(v) > 1) {
int pairs = vMap.get(v);
res += (pairs - 1) * pairs / 2;
}
}
return res;
}
public static void main
(String[] args
) { int[] input = {1, 2, 4, 1, 2, 1, 2, -3, 4, 5, 1, 2, 4, 5, 1, -5, 2 ,5, 6, 7, -3, 7, 8, 2, 1, 2, -3, 4, 5};
int pairCount = new EqualPairCounter().numberOfEqualPairsLinearWithExtraMap(input);
System.
out.
println("Res: " + pairCount
);
// => 52 }
}