I am trying to find an algorithm that returns the number of pairs of duplicates in a list.
Example: Input: [13,4,8,4,13,7,13,9,13] Output: 7 (4 13's comes out to 6 pairs and two 4's comes out to 1 pair )
Can my algorithm become more efficient? I would like it to be faster than Theta(n^2)
Here is what I have:
my_List=[13,3,8,3,13,7,13,9,13]
pairs=0
alreadySeen=[]
for element in my_List:
howMany=0
if element in alreadySeen:
False
else:
howMany=my_List.count(element)
pairs=pairs+((howMany*(howMany-1))/2)
howMany=0
alreadySeen.append(element)
print(pairs)