I was writing code for this problem:
Given an array A having N elements. Find total number of pairs (i,j) such that j < i and Aj = Ai.
This is my code:
raw_input()
l = list(map(int, raw_input().split()))
count = 0
for a in range(len(l)):
count = count + l[a+1:].count(l[a])
print(count)
But unfortunately the code is taking a lot of time. Do you have any suggestions by which I could reduce the time consumption? I mean, how do I reduce the time consumed in the for
loop. I feel that the list.count
method takes a lot of time so do you have any ideas by which I could replace it.