This tutorial will discuss about unique ways to check if a numpy array has duplicates in Python.
Table Of Contents
Method 1: Using numpy.unique()
The numpy
module provides a function unique()
. It accepts a NumPy array as an argument, and returns a new array containing the unique values from the given NumPy array, in sorted order.
So we can use this numpy.unique()
method to check if NumPy array has duplicate elements or not.
Pass the NumPy array to the numpy.unique()
method, and it will return a new NumPy array. Compare the size pf this new array with the original array. If the size of both the arrays are not equal, then it means the original NumPy array has duplicate values.
Frequently Asked:
Let’s see the complete example,
import numpy as np # create a numpy array arr = np.array([34, 22, 56, 22, 89, 76]) # check if numpy array has duplicate values if arr.size != np.unique(arr).size: print("The NumPy Array has duplicates") else: print("The NumPy Array does not have duplicates")
Output
The NumPy Array has duplicates
Method 1: Using Set
A Set in Python, can have only unique values. So we can construct a Set from the NumPy Array. It will discard all the duplicate values and only unique values from NumPy array will be stored in the set.
Now to check if the NumPy array has any duplicate value or not, we can compare the size of the Set and the size of the NumPy array. If their size are not equal, then it means the NumPy array has some duplicate values.
Latest Python - Video Tutorial
Let’s see the complete example,
import numpy as np # create a numpy array arr = np.array([34, 22, 56, 22, 89, 76]) # check if numpy array has duplicate values if len(set(arr)) != len(arr): print("The array has duplicates") else: print("The array does not have duplicates")
Output
The array has duplicates
Summary
we learned about two ways to check if an NumPy array has duplicate values or not. Thanks.
Latest Video Tutorials