Skip to content

Find duplicates in list Python using recursion | Example code

  • by

This example does not find a list of duplicates in list Python using recursion, Only return true false if found duplicates.

Example return true if have duplicates

Return true if found any duplicate else return false.

def check(L):
    if len(L) <= 1:
        return False
    if L[0] == L[1]:
        return True
    if check([L[0]] + L[2:]):
        return True
    if check(L[1:]):
        return True
    return False


print(check([1, 2, 3]))
print(check([1, 2, 2, 3, 3, 3, 1]))

Output:

Find duplicates in list Python using recursion

If you have a solution to get the duplicate element list using recursion in Python, do post in comment we will update it in the example.

Do comment if you have any doubts and suggestions on this Python list tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading