Write a program in Python to check if a series contains duplicate elements or not

PythonPandasServer Side ProgrammingProgramming

Input − Assume, you have the following series,

0    1
1    2
2    3
3    4
4    5

The above series contains no duplicate elements. Let’s verify using the following approaches.

Solution 1

  • Assume, you have a series with duplicate elements

0    1
1    2
2    3
3    4
4    5
5    3
  • Set if condition to check the length of the series is equal to the unique array series length or not. It is defined below,

if(len(data)==len(np.unique(data))):
   print("no duplicates")
else:
   print("duplicates found")

Example

import pandas as pd
import numpy as np
data = pd.Series([1,2,3,4,5])
result = lambda x: "no duplicates" if(len(data)==len(np.unique(data))) else "duplicates found!"
print(result(data))

Output

no duplicates

Solution 2

Example

import pandas as pd
import numpy as np
data = pd.Series([1,2,3,4,5,3])
if(len(data)==len(np.unique(data))):
   print("no duplicates")
else:
   print("duplicates found")

Output

duplicates found!
raja
Published on 10-Feb-2021 17:31:37
Advertisements