# Check if two arrays are equal or not

Given two given arrays of equal length, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though.

Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal.

Examples :

```Input  : arr1[] = {1, 2, 5, 4, 0};
arr2[] = {2, 4, 5, 0, 1};
Output : Yes

Input  : arr1[] = {1, 2, 5, 4, 0, 2, 1};
arr2[] = {2, 4, 5, 0, 1, 1, 2};
Output : Yes

Input : arr1[] = {1, 7, 1};
arr2[] = {7, 7, 1};
Output : No
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

A simple solution is to sort both array and then linearly compare elements.

## C/C++

 `// C++ program to find given two array ` `// are equal or not ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if arr1[0..n-1] and arr2[0..m-1] ` `// contain same elements. ` `bool` `areEqual(``int` `arr1[], ``int` `arr2[], ``int` `n, ``int` `m) ` `{ ` `    ``// If lengths of array are not equal means ` `    ``// array are not equal ` `    ``if` `(n != m) ` `        ``return` `false``; ` ` `  `    ``// Sort both arrays ` `    ``sort(arr1, arr1 + n); ` `    ``sort(arr2, arr2 + m); ` ` `  `    ``// Linearly compare elements ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``if` `(arr1[i] != arr2[i]) ` `            ``return` `false``; ` ` `  `    ``// If all elements were same. ` `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr1[] = { 3, 5, 2, 5, 2 }; ` `    ``int` `arr2[] = { 2, 3, 5, 5, 2 }; ` `    ``int` `n = ``sizeof``(arr1) / ``sizeof``(``int``); ` `    ``int` `m = ``sizeof``(arr2) / ``sizeof``(``int``); ` ` `  `    ``if` `(areEqual(arr1, arr2, n, m)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find given two array ` `// are equal or not ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG { ` `    ``// Returns true if arr1[0..n-1] and arr2[0..m-1] ` `    ``// contain same elements. ` `    ``public` `static` `boolean` `areEqual(``int` `arr1[], ``int` `arr2[]) ` `    ``{ ` `        ``int` `n = arr1.length; ` `        ``int` `m = arr2.length; ` ` `  `        ``// If lengths of array are not equal means ` `        ``// array are not equal ` `        ``if` `(n != m) ` `            ``return` `false``; ` ` `  `        ``// Sort both arrays ` `        ``Arrays.sort(arr1); ` `        ``Arrays.sort(arr2); ` ` `  `        ``// Linearly compare elements ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``if` `(arr1[i] != arr2[i]) ` `                ``return` `false``; ` ` `  `        ``// If all elements were same. ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr1[] = { ``3``, ``5``, ``2``, ``5``, ``2` `}; ` `        ``int` `arr2[] = { ``2``, ``3``, ``5``, ``5``, ``2` `}; ` ` `  `        ``if` `(areEqual(arr1, arr2)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} `

## Python3

 `# Python3 program to find given  ` `# two array are equal or not ` ` `  `# Returns true if arr1[0..n-1] and  ` `# arr2[0..m-1] contain same elements. ` `def` `areEqual(arr1, arr2, n, m): ` ` `  `    ``# If lengths of array are not  ` `    ``# equal means array are not equal ` `    ``if` `(n !``=` `m): ` `        ``return` `False``; ` ` `  `    ``# Sort both arrays ` `    ``arr1.sort(); ` `    ``arr2.sort(); ` ` `  `    ``# Linearly compare elements ` `    ``for` `i ``in` `range``(``0``, n ``-` `1``): ` `        ``if` `(arr1[i] !``=` `arr2[i]): ` `            ``return` `False``; ` ` `  `    ``# If all elements were same. ` `    ``return` `True``; ` ` `  `# Driver Code ` `arr1 ``=` `[``3``, ``5``, ``2``, ``5``, ``2``]; ` `arr2 ``=` `[``2``, ``3``, ``5``, ``5``, ``2``]; ` `n ``=` `len``(arr1); ` `m ``=` `len``(arr2); ` ` `  `if` `(areEqual(arr1, arr2, n, m)): ` `    ``print``(``"Yes"``); ` `else``: ` `    ``print``(``"No"``); ` `     `  `# This code is contributed ` `# by Shivi_Aggarwal. `

## C#

 `// C# program to find given two array ` `// are equal or not ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Returns true if arr1[0..n-1] and ` `    ``// arr2[0..m-1] contain same elements. ` `    ``public` `static` `bool` `areEqual(``int``[] arr1, ` `                                ``int``[] arr2) ` `    ``{ ` `        ``int` `n = arr1.Length; ` `        ``int` `m = arr2.Length; ` ` `  `        ``// If lengths of array are not ` `        ``// equal means array are not equal ` `        ``if` `(n != m) ` `            ``return` `false``; ` ` `  `        ``// Sort both arrays ` `        ``Array.Sort(arr1); ` `        ``Array.Sort(arr2); ` ` `  `        ``// Linearly compare elements ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``if` `(arr1[i] != arr2[i]) ` `                ``return` `false``; ` ` `  `        ``// If all elements were same. ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr1 = { 3, 5, 2, 5, 2 }; ` `        ``int``[] arr2 = { 2, 3, 5, 5, 2 }; ` ` `  `        ``if` `(areEqual(arr1, arr2)) ` `            ``Console.WriteLine(``"Yes"``); ` `        ``else` `            ``Console.WriteLine(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

Output :

```Yes
```

Time Complexity : O(n log n)
Auxiliary Space : O(1)

An Efficient solution of this approach is to use hashing. We store all elements of arr1[] and their counts in a hash table. Then we traverse arr2[] and check if count of every element in arr2[] matches with count in arr1[].

Below is the implementation of above idea. We use unordered_map to store counts.

## C/C++

 `// C++ program to find given two array ` `// are equal or not using hashing technique ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if arr1[0..n-1] and arr2[0..m-1] ` `// contain same elements. ` `bool` `areEqual(``int` `arr1[], ``int` `arr2[], ``int` `n, ``int` `m) ` `{ ` `    ``// If lengths of arrays are not equal ` `    ``if` `(n != m) ` `        ``return` `false``; ` ` `  `    ``// Store arr1[] elements and their counts in ` `    ``// hash map ` `    ``unordered_map<``int``, ``int``> mp; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``mp[arr1[i]]++; ` ` `  `    ``// Traverse arr2[] elements and check if all ` `    ``// elements of arr2[] are present same number ` `    ``// of times or not. ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// If there is an element in arr2[], but ` `        ``// not in arr1[] ` `        ``if` `(mp.find(arr2[i]) == mp.end()) ` `            ``return` `false``; ` ` `  `        ``// If an element of arr2[] appears more ` `        ``// times than it appears in arr1[] ` `        ``if` `(mp[arr2[i]] == 0) ` `            ``return` `false``; ` ` `  `        ``mp[arr2[i]]--; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr1[] = { 3, 5, 2, 5, 2 }; ` `    ``int` `arr2[] = { 2, 3, 5, 5, 2 }; ` `    ``int` `n = ``sizeof``(arr1) / ``sizeof``(``int``); ` `    ``int` `m = ``sizeof``(arr2) / ``sizeof``(``int``); ` ` `  `    ``if` `(areEqual(arr1, arr2, n, m)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find given two array ` `// are equal or not using hashing technique ` `import` `java.util.*; ` `import` `java.io.*; ` ` `  `class` `GFG { ` `    ``// Returns true if arr1[0..n-1] and arr2[0..m-1] ` `    ``// contain same elements. ` `    ``public` `static` `boolean` `areEqual(``int` `arr1[], ``int` `arr2[]) ` `    ``{ ` `        ``int` `n = arr1.length; ` `        ``int` `m = arr2.length; ` ` `  `        ``// If lengths of arrays are not equal ` `        ``if` `(n != m) ` `            ``return` `false``; ` ` `  `        ``// Store arr1[] elements and their counts in ` `        ``// hash map ` `        ``Map map = ``new` `HashMap(); ` `        ``int` `count = ``0``; ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``if` `(map.get(arr1[i]) == ``null``) ` `                ``map.put(arr1[i], ``1``); ` `            ``else` `{ ` `                ``count = map.get(arr1[i]); ` `                ``count++; ` `                ``map.put(arr1[i], count); ` `            ``} ` `        ``} ` ` `  `        ``// Traverse arr2[] elements and check if all ` `        ``// elements of arr2[] are present same number ` `        ``// of times or not. ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``// If there is an element in arr2[], but ` `            ``// not in arr1[] ` `            ``if` `(!map.containsKey(arr2[i])) ` `                ``return` `false``; ` ` `  `            ``// If an element of arr2[] appears more ` `            ``// times than it appears in arr1[] ` `            ``if` `(map.get(arr2[i]) == ``0``) ` `                ``return` `false``; ` ` `  `            ``count = map.get(arr2[i]); ` `            ``--count; ` `            ``map.put(arr2[i], count); ` `        ``} ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr1[] = { ``3``, ``5``, ``2``, ``5``, ``2` `}; ` `        ``int` `arr2[] = { ``2``, ``3``, ``5``, ``5``, ``2` `}; ` ` `  `        ``if` `(areEqual(arr1, arr2)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} `

## Python3

 `# Python3 program to find if given ` `# two arrays are equal or not  ` `# using dictionary ` `from` `collections ``import` `defaultdict ` ` `  `# Returns true if arr1[0..n-1] and  ` `# arr2[0..m-1] contain same elements. ` `def` `areEqual(arr1, arr2, n, m): ` ` `  `    ``# If lengths of array are not  ` `    ``# equal means array are not equal  ` `    ``if` `(n !``=` `m):  ` `        ``return` `False``; ` `         `  `    ``# Create a defaultdict count to  ` `    ``# store counts ` `    ``count ``=` `defaultdict(``int``) ` ` `  `    ``# Store the elements of arr1 ` `    ``# and their counts in the dictionary ` `    ``for` `i ``in` `arr1: ` `        ``count[i] ``+``=` `1` `     `  `    ``# Traverse through arr2 and compare ` `    ``# the elements and its count with  ` `    ``# the elements of arr1 ` `    ``for` `i ``in` `arr2: ` ` `  `        ``# Return false if the elemnent  ` `        ``# is not in arr2 or if any element ` `        ``# appears more no. of times than in arr1 ` `        ``if` `(count[i] ``=``=` `0``): ` `            ``return` `False` `             `  `        ``# If element is found, decrement  ` `        ``# its value in the dictionary ` `        ``else``: ` `            ``count[i] ``-``=` `1` `             `  `    ``# Return true if both arr1 and  ` `    ``# arr2 are equal ` `    ``return` `True` ` `  `# Driver Code ` `arr1 ``=` `[ ``3``, ``5``, ``2``, ``5``, ``2` `] ` `arr2 ``=` `[ ``2``, ``3``, ``5``, ``5``, ``2` `] ` ` `  `n ``=` `len``(arr1)  ` `m ``=` `len``(arr2)  ` ` `  `if` `areEqual(arr1, arr2, n, m):  ` `    ``print``(``"Yes"``)  ` `else``:  ` `    ``print``(``"No"``)  ` ` `  `# This code is contributed by Karthik_Aravind  `

## C#

 `// C# program to find given two array ` `// are equal or not using hashing technique ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG { ` `    ``// Returns true if arr1[0..n-1] and arr2[0..m-1] ` `    ``// contain same elements. ` `    ``public` `static` `bool` `areEqual(``int``[] arr1, ``int``[] arr2) ` `    ``{ ` `        ``int` `n = arr1.Length; ` `        ``int` `m = arr2.Length; ` ` `  `        ``// If lengths of arrays are not equal ` `        ``if` `(n != m) ` `            ``return` `false``; ` ` `  `        ``// Store arr1[] elements and their counts in ` `        ``// hash map ` `        ``Dictionary<``int``, ``int``> map = ``new` `Dictionary<``int``, ``int``>(); ` `        ``int` `count = 0; ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``if` `(!map.ContainsKey(arr1[i])) ` `                ``map.Add(arr1[i], 1); ` `            ``else` `{ ` `                ``count = map[arr1[i]]; ` `                ``count++; ` `                ``map.Remove(arr1[i]); ` `                ``map.Add(arr1[i], count); ` `            ``} ` `        ``} ` ` `  `        ``// Traverse arr2[] elements and check if all ` `        ``// elements of arr2[] are present same number ` `        ``// of times or not. ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``// If there is an element in arr2[], but ` `            ``// not in arr1[] ` `            ``if` `(!map.ContainsKey(arr2[i])) ` `                ``return` `false``; ` ` `  `            ``// If an element of arr2[] appears more ` `            ``// times than it appears in arr1[] ` `            ``if` `(map[arr2[i]] == 0) ` `                ``return` `false``; ` ` `  `            ``count = map[arr2[i]]; ` `            ``--count; ` ` `  `            ``if` `(!map.ContainsKey(arr2[i])) ` `                ``map.Add(arr2[i], count); ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int``[] arr1 = { 3, 5, 2, 5, 2 }; ` `        ``int``[] arr2 = { 2, 3, 5, 5, 2 }; ` ` `  `        ``if` `(areEqual(arr1, arr2)) ` `            ``Console.WriteLine(``"Yes"``); ` `        ``else` `            ``Console.WriteLine(``"No"``); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output :

`Yes`

Time Complexity : O(n)
Auxiliary Space : O(n)

This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.