ans
to 0, which will hold the count of good pairs.counts
to keep track of the frequency of each number.num
in the input array nums
.
num
, increment ans
by counts[num]
(the number of times num
has been seen before).counts[num]
by 1.ans
.