HackerRank 'Sherlock and Pairs' Solution | MartinKysel.com
Sherlock is given an array of N integers A0, A1 … AN-1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj.
That is, Sherlock has to count total number of pairs of indices (i, j) where Ai = Aj AND i ≠ j.
Read full article from HackerRank 'Sherlock and Pairs' Solution | MartinKysel.com
Sherlock is given an array of N integers A0, A1 … AN-1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj.
That is, Sherlock has to count total number of pairs of indices (i, j) where Ai = Aj AND i ≠ j.
def
cnt_equals(arr):
the_map
=
{}
final_cnt
=
0
for
value
in
arr:
if
value
not
in
the_map:
the_map[value]
=
1
else
:
the_map[value]
+
=
1
for
value
in
the_map.values():
if
value !
=
1
:
final_cnt
+
=
(value
*
(value
-
1
))
return
final_cnt