Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
NumPy| How to get the unique elements of an Array
Next article icon

NumPy| How to get the unique elements of an Array

Last Updated : 09 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python.

It returns unique elements in a new sorted array.

Example:

Python3
import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) 

Output:

[1 2 3 4 5]

Syntax

Syntax: np.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)

Parameters

  • ar: Input array. 
  • return_index: If True, also return the indices of ar that result in the unique array.
  • return_inverse: If True, also return the indices of the unique array that can be used to reconstruct ar.
  • return_counts: If True, also return the number of times each unique item appears in ar.
  • axis: The axis to operate on. If None, ar will be flattened.

Returns: Return the unique of an array. 

Let us look at some examples of how to get the unique elements of an array using NumPy library:

More Examples

Here we will see examples of how to find unique elements in 1D and 2D NumPy arrays.

Example 1: Finding Unique Elements in a 1D Numpy Array

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: Finding Unique Elements in a 2D Numpy Array

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]   

Next Article
NumPy| How to get the unique elements of an Array

Y

ysachin2314
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-program
  • Python numpy-arrayManipulation
Practice Tags :
  • python

Similar Reads

    How to count the frequency of unique values in NumPy array?
    Let's see How to count the frequency of unique values in the NumPy array. Python’s Numpy library provides a numpy.unique() function to find the unique elements and their corresponding frequency in a NumPy array. numpy.unique() Syntax Syntax: numpy.unique(arr, return_counts=False) Return: Sorted uniq
    4 min read
    How to Remove Duplicate Elements from NumPy Array
    In this article, we will see how to remove duplicate elements from NumPy Array. Here we will learn how to Remove Duplicate Elements from a 1-D NumPy Array and 2-D NumPy Array.Input1: [1 2 3 4 5 1 2 3 1 2 9] Output1: [1 2 3 4 5 9] Explanation: In this example, we have removed duplicate elements from
    7 min read
    Find unique rows in a NumPy array
    Finding unique rows means removing duplicate rows from a 2D array so that each row appears only once. For example, given [[1, 2], [3, 4], [1, 2], [5, 6]], the unique rows would be [[1, 2], [3, 4], [5, 6]]. Let’s explore efficient ways to achieve this using NumPy.Using np.unique(axis=0)np.unique(axis
    3 min read
    NumPy ndarray.size() Method | Get Number of Elements in NumPy Array
    The ndarray.size() method returns the number of elements in the NumPy array. It works the same as np.prod(a.shape), i.e., the product of the dimensions of the array. Example Python3 import numpy as np arr = np.zeros((3, 4, 2), dtype = np.complex128) gfg = arr.size print (gfg) Output : 24Syntax Synta
    1 min read
    Python | Numpy np.unique() method
    numpy.unique() finds the unique elements of an array. It is often used in data analysis to eliminate duplicate values and return only the distinct values in sorted order. Example:Pythonimport numpy as np a = np.array([1, 2, 2, 3, 4, 4, 4]) res = np.unique(a) print(res)Output[1 2 3 4] Explanation: nu
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences