human
Give me a code to count of index pairs with equal elements in an array and the other steps required given the following:
Count of index pairs with equal elements in an array | Set 2 | Function that count the pairs having same elements in the array arr [ ].
Hash map to keep track of occurences of elements.
Traverse the array arr [ ].
Check if occurence of arr [ i ] > 0 add count [ arr [ i ] ] to answer.
Return the result.
Given array arr [ ].
Function call"
Solution in Python:
assistant
#Here is code to count of index pairs with equal elements in an array and the other steps required.
 def countPairs ( arr , n ) :
	ans = 0
	count = { }
	for i in range ( n ) :
		if arr [ i ] in count :
			ans += count [ arr [ i ] ]
		if arr [ i ] in count :
			count [ arr [ i ] ] += 1
		else :
			count [ arr [ i ] ] = 1
	return ans
arr = [ 1 , 2 , 1 , 1 ]
N = len ( arr )
print ( countPairs ( arr , N ) )