+ 3
How to find duplicate of a number in an array ?
with less time complexity
3 Answers
+ 7
@Deep Serket thanks, I'm still learning. just tried to help
+ 1
Convert your array into Set.
myset = set(mylist)
0
remove duplicates:
mylist = list(set(mylist))
find duplicates:
for elem in mylist:
if mylist.count(elem) > 1:
pass
or if you want to store all duplicates:
uniques = set(mylist)
duplicates = [elem for elem in mylist if elem not in uniques or uniques.remove(elem)]
@LayB "for i in range(len(array))" is a bad idea for a python programmer.
http://docs.python-guide.org/en/latest/writing/style/