I have the below code. It's working except when I have a duplicate that repeats 3 or more times, the program is considering that element as a new number and displaying it twice. How to fix this issue?
import collections
mylist = [1,2,3,4,5,5,6,7,7,7,8,9]
i = 0
count = 0
for i in range(len(mylist)):
print1 = mylist.count(mylist[i])
if print1 > 1:
print("Duplicate Element: " , mylist[i])
print("It is repeated " , print1, "times")
mylist.remove(mylist[i])
count += 1
i += 1
The output I am getting is
Duplicate Element: 5
It is repeated 2 times
Duplicate Element: 7
It is repeated 3 times
Duplicate Element: 7
It is repeated 2 times
Traceback (most recent call last):
File "C:/Users/sanantha/Documents/Python/Lab6/task3.py", line 10, in <module>
print1 = mylist.count(mylist[i])
IndexError: list index out of range
How to fix this?