Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to get the unique elements of an array using NumPy?

  • Last Updated : 31 Aug, 2021

Let’s see How to get the unique elements of an array using NumPy. So for finding unique elements from the array we are using numpy.unique() function of NumPy library.

Syntax: np.unique(Array)
Return: Return the unique of an array. 

Now, Let’s see the examples:

Example 1:

Python3




# import library
import numpy as np            
 
# create 1d-array
arr = np.array([3, 3, 4,
                5, 6, 5,
                6, 4])
 
# find unique element
# from a array
rslt = np.unique(arr)
 
print(rslt)

Output:

[3 4 5 6]

Example 2:

Python3




# import library
import numpy as np
 
# create a numpy 2d-array
arr = np.array([[9, 9, 7, 7],
              [3, 4, 3, 4]])
 
# find unique element
# from a array
rslt = np.unique(arr)
 
print(rslt)

Output:

[3 4 7 9] 
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!