Posted on 20th April 2024
Now let us check if there are any duplicates in this list below
Lostofelements = ['ok', 'good', 'better', 'good', 'excellent', 'ok']
We can check for duplicates by the below steps
Example:
First, we create a function which follows the before mentioned algorithm
def checkDuplicates_1(listOfElems):
''' Check if given list have any duplicates '''
if len(listOfElems) == len(set(listOfElems)):
return False
else:
return True
Then in the second step lest us check for the duplicates
listOfElems = [‘ok’, ‘good’, ‘better’, ‘good’, ‘excellent’, ‘ok’]
result = checkIfDuplicates_1(listOfElems)
if result:
print('Yes, list have duplicates')
else:
print('there are no duplicates in the list')
Output: Yes, list have duplicates
STILL GOT QUERIES?