VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-two-arrays-are-equal-or-not/

⇱ Check if two arrays are equal or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if two arrays are equal or not

Last Updated : 5 Feb, 2026

Given two arrays, a[] and b[] of equal length. The task is to determine if the given arrays are equal or not. Two arrays are considered equal if:

  • Both arrays contain the same set of elements.
  • The arrangements (or permutations) of elements may be different.
  • If there are repeated elements, the counts of each element must be the same in both arrays.

Examples:

Input: a[] = [1, 2, 5, 4, 0], b[] = [2, 4, 5, 0, 1]
Output: true

Input: a[] = [1, 2, 5, 4, 0, 2, 1], b[] = [2, 4, 5, 0, 1, 1, 2] 
Output: true

Input: a[] = [1, 7, 1], b[] = [7, 7, 1]
Output: false

[Naive Approach] Using Sorting - O(n*log n) Time and O(1) Space

The basic idea is to sort the both arrays. Compare the arrays element by element one by one if all are same then it is equal array otherwise it is not.


Output
true

[Expected Approach] Using Hashing- O(n) Time and O(n) Space

Use of a hash map to count the occurrences of each element in one array and then verifying these counts against the second array.


Output
true
Comment
Article Tags: