Check If there are Duplicates in a List

Python Check If there are Duplicates in a List

Lists are similar to dynamically sized arrays (e.g., vector in C++ and ArrayList in Java) that are declared in other languages. Lists don’t always have to be homogeneous, which makes them a useful tool in Python. Integers, Strings, and Objects are all DataTypes that can be combined into a single list. Lists are mutable, meaning they can be modified after they’ve been formed.

Duplicates are integers, strings, or items in a list that are repeated more than once.

Given a list, the task is to check whether it has any duplicate element in it.

Examples:

Input:

givenlist=["hello", "this", "is", "BTechGeeks" , "hello"]

Output:

True

Explanation:

hello is repeated twice so the answer is Yes

Check whether list contains any repeated values

There are several ways to check duplicate elements some of them are:

Method #1 :Using list and count() function

The list class in Python has a method count() that returns the frequency count of a given list element.

list.count(Element)

It returns the number of times an element appears in the list.

Approach:

The idea is to iterate over all of the list’s elements and count the number of times each element appears.

If the count is greater than one, this element has duplicate entries.

Below is the implementation:

# function which return true if duplicates are present in list else false
def checkDuplicates(givenlist):
    # Traverse the list
    for element in givenlist:
        # checking the count/frequency of each element
        if(givenlist.count(element) > 1):
            return True
    # if the above loop do not return anuthing then there are no duplicates
    return False

#Driver code
# Given list
givenlist = ["hello", "this", "is", "BTechGeeks", "hello"]
# passing this list to checkDuplicates function
print(checkDuplicates(givenlist))

Output:

True

Time Complexity : O(n^2)

Method #2 : Using set()

Follow the steps below to see if a list contains any duplicate elements.

If the list does not contain any unhashable objects, such as list, use set().

When a list is passed to set(), the function returns set, which ignores duplicate values and keeps only unique values as elements..

Using the built-in function len(), calculate the number of elements in this set and the original list and compare them.

If the number of elements is the same, there are no duplicate elements in the original list ,if the number of elements is different, there are duplicate elements in the original list.

The following is the function that returns False if there are no duplicate elements and True if there are duplicate elements:

# function which return true if duplicates are present in list else false
def checkDuplicates(givenlist):
    # convert given list to set
    setlist = set(givenlist)
    # calculate length of set and list
    setlength = len(setlist)
    listlength = len(givenlist)
    # return the comparision between set length and list length
    return setlength != listlength


# Driver code
# Given list
givenlist = ["hello", "this", "is", "BTechGeeks", "hello"]
# passing this list to checkDuplicates function
print(checkDuplicates(givenlist))

Output:

True

Time Complexity : O(n(log(n))

Method #3: Using Counter() function from collections (Hashing)

Calculate the frequencies of all elements using Counter() function which will be stored as frequency dictionary.

If the length of frequency dictionary is equal to length of list then it has no duplicates.

Below is the implementation:

# importing Counter function from collections
from collections import Counter

# function which return true if duplicates are present in list else false


def checkDuplicates(givenlist):
    # Calculating frequency using counter() function
    frequency = Counter(givenlist)
    # compare these two lengths and return it
    return len(frequency) != len(givenlist)


# Driver code
# Given list
givenlist = ["hello", "this", "is", "BTechGeeks", "hello"]
# passing this list to checkDuplicates function
print(checkDuplicates(givenlist))

Output:

True

Time Complexity : O(n)
Related Programs:

Related Programs: