Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,819 answers

573 users

How to implement a function that check if there are duplicates in a list in Python

3 Answers

0 votes
def has_duplicates(lst):
    for i in range(0, len(lst)):
        for j in range(i + 1, len(lst)):
            if lst[i] == lst[j]:
                return True
    return False


lst1 = ["python", "java", "python", "c", "c", "php", "c"]

print(has_duplicates(lst1))

lst2 = ["python", "java", "c", "php", "swift"]

print(has_duplicates(lst2))



'''
run:

True
False

'''

 



answered Oct 24, 2020 by avibootz
0 votes
def has_duplicates(lst):
    if len(lst) == len(set(lst)):
        return False
    else:
        return True


lst1 = ["python", "java", "python", "c", "c", "php", "c"]

print(has_duplicates(lst1))

lst2 = ["python", "java", "c", "php", "swift"]

print(has_duplicates(lst2))



'''
run:

True
False

'''

 



answered Oct 24, 2020 by avibootz
0 votes
def has_duplicates(lst):
    st = set()
    
    for element in lst:
        if element in st:
            return True
        else:
            st.add(element)   
            
    return False


lst1 = ["python", "java", "python", "c", "c", "php", "c"]

print(has_duplicates(lst1))

lst2 = ["python", "java", "c", "php", "swift"]

print(has_duplicates(lst2))



'''
run:

True
False

'''

 



answered Oct 24, 2020 by avibootz
...