How to Remove Duplicate Elements from NumPy Array
Last Updated :
11 Jun, 2025
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 our input NumPy Array
Input2: [ [1 2 3 4 5]
[0 1 2 9 8]
[1 4 9 8 5]
[1 2 3 4 5]
[0 1 2 9 8] ]
Output2: [[0 1 2 9 8]
[1 2 3 4 5]
[1 4 9 8 5]]
Explanation: In this example, we have removed duplicate elements from 2-D NumPy Array
Remove Duplicate Element from 1-D NumPy Array
Below are the methods by which we can remove duplicate elements from 1-D NumPy Array in Python:
- Using set()
- Using numpy unique
- Using iteration
Remove Duplicate Element using Python set()
We all knew that set() has a unique property that it eliminates duplicate elements. So in this example, we are using this property to remove our duplicate elements from our 1-D NumPy Array.
Python
import numpy as np
#declaring original array
org = np.array([1, 2, 3, 4, 5, 1, 2, 3, 1, 2 , 9])
#displaying the original array
print("Original Array : ")
print(org,"\n")
#using the set operation
new = set(org)
new = np.array(list(new))
#displaying the new array with updated/unique elements
print("New Array : ")
print(new)
Output
Original Array :
[1 2 3 4 5 1 2 3 1 2 9]
New Array :
[1 2 3 4 5 9]
Time complexity: O(n), where n is the number of elements
Auxiliary Space: O(n), where n is the number of elements
Remove Duplicate Elements using numpy unique()
In this example, we are using numpy.unique() function to remove the duplicate elements from the NumPy Array.
Python
import numpy as np
#declaring original array
org = np.array([1, 2, 3, 4, 5, 1, 2, 3, 1, 2 , 9])
#displaying the original array
print("Original Array : ")
print(org,"\n")
#using the .unique()
new = np.unique(org)
#displaying the new array with updated/unique elements
print("New Array : ")
print(new)
Output
Original Array :
[1 2 3 4 5 1 2 3 1 2 9]
New Array :
[1 2 3 4 5 9]
Time complexity: O(N*log N)
Auxiliary Space: O(N)
Remove Duplicate Elements using iteration
In this example, we are using iteration to remove duplicate elements from NumPy Array.
Python
import numpy as np
#declaring original array
org = np.array([1, 2, 3, 4, 5, 1, 2, 3, 1, 2 , 9])
#displaying the original array
print("Original Array : ")
print(org,"\n")
l = []
for i in org:
if i not in l:
l.append(i)
new = np.array(l)
#displaying the new array with updated/unique elements
print("New Array : ")
print(new)
Output
Original Array :
[1 2 3 4 5 1 2 3 1 2 9]
New Array :
[1 2 3 4 5 9]
Time complexity : O(N*M) where N is the number of elements and M is the number of unique elements in the array.
Auxiliary Space : O(N) where N is the number of elements in the array.
Remove Duplicate Element in 2-D NumPy Array
Below are the methods by which we can remove duplicate element from 1-D NumPy Array:
- Using numpy unique
- Using set()
- Using iteration
- Using numpy.lexsort() and np.diff()
Remove Duplicate Element Using np.unique()
In this example, we are using numpy.unique() function to remove the duplicate elements from the NumPy Array. It is as same as we used in 1-D NumPy Array but the difference is that we have to specify that we want to remove only duplicate rows and not the columns. So we have specify that by sing axis=0 that we want only duplicate rows to be remove without hampering column data.
Python
import numpy as np
org = np.array([
[1,2,3,4,5],
[0,1,2,9,8],
[1,4,9,8,5],
[1,2,3,4,5],
[0,1,2,9,8]])
#displaying the original array
print("Original Array : ")
print(org,"\n")
new = [list(i) for i in org]
#using the .unique() with axis=0
new = np.unique(new,axis=0)
#displaying the new array with updated/unique elements
print("New Array : ")
print(new)
Output
Original Array :
[[1 2 3 4 5]
[0 1 2 9 8]
[1 4 9 8 5]
[1 2 3 4 5]
[0 1 2 9 8]]
New Array :
[[0 1 2 9 8]
[1 2 3 4 5]
[1 4 9 8 5]]
Time complexity : O(NM * log(NM))
Auxiliary Space : O(NM)
Remove Duplicate Element in 2-D NumPy Array Using set().
In this example, we are using set() same as we use in the 1-D array but with frozenset() to preserve the order.
Python
import numpy as np
org = np.array([
[1,2,3,4,5],
[0,1,2,9,8],
[1,4,9,8,5],
[1,2,3,4,5],
[0,1,2,9,8]])
#displaying the original array
print("Original Array : ")
print(org,"\n")
# Convert each row of original array to a frozenset
rows = set()
for i in org:
rows.add(frozenset(i))
# Convert the frozensets back to NumPy array
new = np.array([list(i) for i in rows])
#displaying the new array with updated/unique elements
print("New Array : ")
print(new)
Output
Original Array :
[[1 2 3 4 5]
[0 1 2 9 8]
[1 4 9 8 5]
[1 2 3 4 5]
[0 1 2 9 8]]
New Array :
[[1 2 3 4 5]
[1 4 5 8 9]
[0 1 2 8 9]]
Time complexity : O(N*M) where N is the number of rows and M is the number of columns in the original array.
Auxiliary Space : O(N*M) where N is the number of rows and M is the number of columns in the original array.
Remove Duplicate Element in 2-D NumPy Array Using iteration
We can also use iteration same as we have done in 1-D part. It is one of the simplest or naive approach. We can simply iterate through the 2-D numpy array and remove the element that appear more than one time.
Python
import numpy as np
org = np.array([
[1,2,3,4,5],
[0,1,2,9,8],
[1,4,9,8,5],
[1,2,3,4,5],
[0,1,2,9,8]])
#displaying the original array
print("Original Array : ")
print(org,"\n")
new = [] #defining a new array
#iterating through each element of org array
for i in org:
if list(i) not in new:
new.append(list(i))
new = np.array(new)
#displaying the new array with updated/unique elements
print("New Array : ")
print(new)
Output
Original Array :
[[1 2 3 4 5]
[0 1 2 9 8]
[1 4 9 8 5]
[1 2 3 4 5]
[0 1 2 9 8]]
New Array :
[[1 2 3 4 5]
[0 1 2 9 8]
[1 4 9 8 5]]
Time complexity : O(N*M*R) where N is the number of rows, M is the number of columns and R is the number of unique rows in array.
Auxiliary Space : O(N*M) where N is the number of rows and M is the number of columns in the original array.
Remove Duplicate Element Using numpy.lexsort() and np.diff()
We can use numpy.lexsort() and np.diff() to eliminate the repeated elements. The numpy.lexsort() returns the array of indices of the elements from smallest to largest. This can be later used to obatin the sorted array. The np.diff() is used to compute the Nth order discrete difference between the consecutive elements of the passed array.
Python
org = np.array([
[1,2,3,4,5],
[0,1,2,9,8],
[1,4,9,8,5],
[1,2,3,4,5],
[0,1,2,9,8]])
#displaying the original array
print("Original Array : ")
print(org,"\n")
def remove_dupl_arrays(array:np.array,tol:float)->np.array:
"""removes an array without the duplicates from a list of list, given the tolerance tol"""
new = np.lexsort(org.T) #pssing transpose of org array to lexsort()
new01 = org[new,:] #ordered org
ind_double = np.concatenate(([True], np.linalg.norm(np.diff(new01, axis=0),axis=1)<tol))
return np.array(new01[ind_double])
#displaying the new array with updated/unique elements
print("Result Array : ")
print(remove_dupl_arrays(org,1e-5))
Output
Original Array :
[[1 2 3 4 5]
[0 1 2 9 8]
[1 4 9 8 5]
[1 2 3 4 5]
[0 1 2 9 8]]
Result Array :
[[1 2 3 4 5]
[1 4 9 8 5]
[0 1 2 9 8]]
Time complexity : O(N * M) (not O(M * N * log(N))) where N is the number of rows, M is the number of columns and R is the number of unique rows in array.
Auxiliary Space : O(N*M) where N is the number of rows and M is the number of columns in the original array.
Similar Reads
How to Remove Duplicate Elements from Array in Ruby? This article focuses on discussing how to remove duplicate elements from an array in Ruby. In Ruby, an array is a fundamental data structure that stores a collection of elements in a specific order. Various methods and approaches can be used to remove duplicate elements from an array. Removing Dupli
2 min read
How to remove specific elements from a NumPy array ? In this article, we will discuss how to remove specific elements from the NumPy Array. Remove specific elements from a NumPy 1D arrayDeleting element from NumPy array using np.delete() The delete(array_name ) method will be used to do the same. Where array_name is the name of the array to be delete
3 min read
Golang Program that Removes Duplicate Elements From the Array Arrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequen
4 min read
Python Set difference to find lost element from a duplicated array Given two arrays which are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element. Examples: Input: A = [1, 4, 5, 7, 9] B = [4, 5, 7, 9] Output: [1] 1 is missing from second array. Input: A = [2, 3, 4, 5 B = 2, 3, 4, 5,
1 min read
How to Splice Duplicate Item from JavaScript Array? Given an array of numbers or strings containing some duplicate values, the task is to remove these duplicate values from the array without creating a new array or storing the duplicate values anywhere else.Examples: Input: arr = [1, 2, 3, 4, 3, 2, 1];Output: [1,2,3,4]Input: [1, 4, 6, 1, 2, 5, 2, 1,
4 min read
NumPy| How to get the unique elements of an Array 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
2 min read
How to remove rows from a Numpy array based on multiple conditions ? In this article, we will learn how to remove rows from a NumPy array based on multiple conditions. For doing our task, we will need some inbuilt methods provided by the NumPy module which are as follows: np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on g
3 min read
Ways to remove duplicates from list in Python In this article, we'll learn several ways to remove duplicates from a list in Python. The simplest way to remove duplicates is by converting a list to a set.Using set()We can use set() to remove duplicates from the list. However, this approach does not preserve the original order.Pythona = [1, 2, 2,
2 min read
How to remove NaN values from a given NumPy array? In this article, we are going to learn how to remove Nan values from a given array. Nan values are those values that do not have a specific value associated with them or they are different from the type of values that are to be used in the declared array. There are basically three approaches with sl
3 min read
How to compare two NumPy arrays? Here we will be focusing on the comparison done using NumPy on arrays. Comparing two NumPy arrays determines whether they are equivalent by checking if every element at each corresponding index is the same. Method 1: We generally use the == operator to compare two NumPy arrays to generate a new arr
2 min read