Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Check if two arrays are equal
Arrays are fundamental data structures in Python, and checking if two arrays are equal is a common operation. Python provides several techniques to compare arrays based on whether they contain the same elements, regardless of order.
Understanding Array Equality
Array equality can be checked in two ways:
Element-wise comparison ? Same elements at same positions
Set-based comparison ? Same elements regardless of order
Let's explore different methods to check array equality ?
Method 1: Using NumPy for Element-wise Comparison
NumPy's array_equal() function compares arrays element by element ?
import numpy as np
arr1 = [1, 2, 3, 4, 5]
arr2 = [1, 2, 3, 4, 5]
arr3 = [5, 4, 3, 2, 1]
# Element-wise comparison
print("arr1 == arr2:", np.array_equal(arr1, arr2))
print("arr1 == arr3:", np.array_equal(arr1, arr3))
# Using == operator with all()
result = (np.array(arr1) == np.array(arr2)).all()
print("Using == with all():", result)
arr1 == arr2: True arr1 == arr3: False Using == with all(): True
Method 2: Using Sorting for Content Comparison
Sort both arrays and compare element by element to check if they contain the same elements ?
def arrays_equal_sorted(arr1, arr2):
if len(arr1) != len(arr2):
return False
# Create copies to avoid modifying original arrays
sorted_arr1 = sorted(arr1)
sorted_arr2 = sorted(arr2)
return sorted_arr1 == sorted_arr2
# Test with different arrays
arr1 = [1, 3, 5, 2, 4]
arr2 = [5, 4, 3, 2, 1]
arr3 = [1, 2, 3, 4, 6]
print("arr1 and arr2:", arrays_equal_sorted(arr1, arr2))
print("arr1 and arr3:", arrays_equal_sorted(arr1, arr3))
arr1 and arr2: True arr1 and arr3: False
Method 3: Using Sets for Unique Element Comparison
Convert arrays to sets to check if they contain the same unique elements ?
def arrays_equal_sets(arr1, arr2):
return set(arr1) == set(arr2)
# Test arrays
arr1 = [1, 2, 3, 2, 1]
arr2 = [3, 1, 2, 1, 2]
arr3 = [1, 2, 4]
print("arr1 and arr2 (same elements):", arrays_equal_sets(arr1, arr2))
print("arr1 and arr3 (different elements):", arrays_equal_sets(arr1, arr3))
# Note: Sets ignore duplicates
arr4 = [1, 1, 2, 2]
arr5 = [1, 2]
print("Sets ignore duplicates:", arrays_equal_sets(arr4, arr5))
arr1 and arr2 (same elements): True arr1 and arr3 (different elements): False Sets ignore duplicates: True
Method 4: Using Counter for Frequency Comparison
Use Counter from collections to compare element frequencies ?
from collections import Counter
def arrays_equal_counter(arr1, arr2):
return Counter(arr1) == Counter(arr2)
# Test with duplicate elements
arr1 = [1, 2, 2, 3, 3, 3]
arr2 = [3, 3, 2, 1, 2, 3]
arr3 = [1, 2, 3, 3, 3] # Different frequency
print("Same frequencies:", arrays_equal_counter(arr1, arr2))
print("Different frequencies:", arrays_equal_counter(arr1, arr3))
Same frequencies: True Different frequencies: False
Comparison of Methods
| Method | Order Matters | Handles Duplicates | Best For |
|---|---|---|---|
| NumPy array_equal | Yes | Yes | Exact element-wise comparison |
| Sorting | No | Yes | Same elements, any order |
| Sets | No | No (ignores) | Unique elements only |
| Counter | No | Yes | Element frequency comparison |
Conclusion
Choose numpy.array_equal() for exact element-wise comparison, sorted() comparison for same elements in any order, or Counter when element frequency matters. Sets are ideal when only unique elements need to be compared.
