0

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])
2
  • len() and range() are built-in functions. Can we use set()? Commented Oct 25, 2015 at 21:19
  • I'm not entirely sure. Would it be more difficult without set()?. Otherwise it'd simply be if len(list)!=len(set(list)): print True. else: print False? Commented Oct 25, 2015 at 21:21

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

I really overcomplicated this. Thank you so much.
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.