Instagram
youtube
Facebook
Twitter

Find Unique Elements in a NumPy Array

A Find Unique Elements in a NumPy Array?

Code Explanation:
Library Import:
numpy is imported as np to work with arrays and NumPy functions.
Array Creation: A 1D array arr is created containing some repeated elements.
Finding Unique Values: np.unique(arr) extracts all the unique elements from the array.
Return Type: It returns a new sorted array with only distinct values from arr.
Store Result: The unique values are stored in the variable unique_elements.
Display Output: print(unique_elements) prints the unique elements: [1 2 3 4].

 

Program:

import numpy as np

arr = np.array([1, 2, 2, 3, 4, 4])

unique_elements = np.unique(arr)

print(unique_elements)