+ 3

How to find duplicate of a number in an array ?

with less time complexity

9th Mar 2017, 2:05 PM
charumathi.n
charumathi.n - avatar
3 Answers
+ 7
@Deep Serket thanks, I'm still learning. just tried to help
9th Mar 2017, 11:23 PM
LayB
LayB - avatar
+ 1
Convert your array into Set. myset = set(mylist)
9th Mar 2017, 2:18 PM
Felipe Cruz
Felipe Cruz - avatar
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/
9th Mar 2017, 6:30 PM
Deep Serket
Deep Serket - avatar