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

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



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

[3 4 5 6]

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# imoprt 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)

chevron_right


Output:

[3 4 7 9]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.