Reputation: 55
I'm trying to find if there is a duplicate within a list without using the in-built functions. So far I have something like this however it does not seem to be working. Can anyone help? Give pointers? Improvements? I'd appreciate it. (Python version 2.7.10)
def DupSearch(list):
counter=0
for i in range(len(list)):
if list[i]==list[0]:
for j in range(len(list)):
if list[j]!=list[j+i]:
print "No duplicated"
else:
counter=counter+1
if counter == len(list):
print "Duplicate found"
DupSearch([1,2,3,4,5,3])
Upvotes: 0
Views: 4773
Reputation: 11134
You can use a dictionary to store the frequency of individual element,
lst=[1,3,5,7,6]
dic={}
for i in lst:
if i in dic.keys():
dic[i]+=1
else:
dic[i]=1
duplicate= False
for key,value in dic.iteritems():
if value>1:
duplicate = True
print "List has duplicate element"
break
if not duplicate:
print "No duplicate element"
Upvotes: 0
Reputation: 18917
Something like this?
def DupSearch(list):
for i in range(len(list)):
for j in range(i+1, len(list)):
if list[i]==list[j]:
print "Duplicate found"
return
print "No duplicated"
Upvotes: 1