# How to check if a number is repeated in a list python – Python Check If there are Duplicates in a List

How to check if a number is repeated in a list python: 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

Python list check for duplicates: There are several ways to check duplicate elements some of them are:

### Method #1 :Using list and count() function

Python check list for duplicates: 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()

Python check for duplicates in list: 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)

Python check duplicates in list: 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: