Determining duplicate values in an array

Learn, how to determine duplicate values in an array in Python? By Pranit Sharma Last updated : October 10, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

In a NumPy array, duplicate values are those values that occur more than once.

Problem statement

Suppose that we are given a NumPy array that contains some integer values (repeating) and we need to which elements of this array are repeated.

NumPy Array - Determining Duplicate Values

There are many methods with the help of which we can find out the duplicate values and one of the easiest methods is converting the array into a pandas series and using series.duplicated() method. This method will return True if the value is duplicated; other-wise, False.

Let us understand with the help of an example,

Python program to determine duplicate values in an array

# Import numpy
import numpy as np

# Import pandas
import pandas as pd

# Creating a numpy array
arr = np.array([10,20,10,40,20,60,70,70,10,100])

# Display original array
print("Original array:\n",arr,"\n")

# Converting array into pandas series
s = pd.Series(arr)

# Finding duplicates
res = s.duplicated()

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Determining duplicate values in an array

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.