GFG App
Open App
Browser
Continue

# Check if two arrays are equal or not

Given two arrays, arr1 and arr2 of equal length N, the task is to find if the given arrays are equal or not.

Two arrays are said to be equal if:

• both of them contain the same set of elements,
• arrangements (or permutations) of elements might/might not be same.
• If there are repetitions, then counts of repeated elements must also be the same for two arrays 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

## Check if two arrays are equal or not using Sorting

Follow the steps below to solve the problem using this approach:

• Sort both the arrays
• Then linearly compare elements of both the arrays
• If all are equal then return true, else return false

Below is the implementation of the above approach:

## 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's 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``);`   `    ``// Function call` `    ``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` `};`   `        ``// Function call` `        ``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):` `        ``if` `(arr1[i] !``=` `arr2[i]):` `            ``return` `False`   `    ``# If all elements were same.` `    ``return` `True`     `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `    ``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"``)`

## C#

 `// C# program for the above approach` `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's code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int``[] arr1 = { 3, 5, 2, 5, 2 };` `        ``int``[] arr2 = { 2, 3, 5, 5, 2 };`   `        ``// Function call` `        ``if` `(areEqual(arr1, arr2))` `            ``Console.WriteLine(``"Yes"``);` `        ``else` `            ``Console.WriteLine(``"No"``);` `    ``}` `}`   `// This code is contributed by anuj_67.`

## PHP

 ``

## Javascript

 ``

Output

`Yes`

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)

## Check if two arrays are equal or not using Hashing

Store count of all elements of arr1[] in a hash table. Then traverse arr2[] and check if the count of every element in arr2[] matches with the count of elements of arr1[].

Follow the steps mentioned below to implement the approach:

• First check if length of arr1 is not equal to the length of arr2 then return false
• Then traverse over first array and store the count of every element in the hash map
• Then traverse over second array and decrease the count of its elements in the hash map. If that element is not present or the count of that element is
zero in the hash map, then return false, else decrease the count of that element
• Return true at the end, as both the arrays are equal by now

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#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``;` `        ``// decrease the count of arr2 elements in the` `        ``// unordered map` `        ``mp[arr2[i]]--;` `    ``}`   `    ``return` `true``;` `}`   `// Driver's 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``);`   `    ``// Function call` `    ``if` `(areEqual(arr1, arr2, N, M))` `        ``cout << ``"Yes"``;` `    ``else` `        ``cout << ``"No"``;` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `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 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's code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `arr1[] = { ``3``, ``5``, ``2``, ``5``, ``2` `};` `        ``int` `arr2[] = { ``2``, ``3``, ``5``, ``5``, ``2` `};`   `        ``// Function call` `        ``if` `(areEqual(arr1, arr2))` `            ``System.out.println(``"Yes"``);` `        ``else` `            ``System.out.println(``"No"``);` `    ``}` `}`

## Python3

 `# Python3 program for the above approach`   `# Returns true if arr1[0..N-1] and` `# arr2[0..M-1] contain same elements.`     `def` `is_arr_equal(arr1, arr2):` `        ``# Check if the length of arrays are` `    ``# equal or not: A Easy Logic Check` `    ``if` `len``(arr1) !``=` `len``(arr2):` `        ``return` `False`   `    ``# Create a dict named count to` `    ``# store counts of each element` `    ``count ``=` `{}` `    ``# Store the elements of arr1` `    ``# and their counts in the dictionary` `    ``for` `i ``in` `arr1:` `        ``if` `i ``in` `count:` `                ``# Element already in dict, simply increment its count` `            ``count[i] ``+``=` `1` `        ``else``:` `                ``# Element found for first time, initialize it with value 1.` `            ``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 element` `        ``# is not in count or if any element` `        ``# appears more no. of times than in arr1` `        ``if` `i ``not` `in` `count ``or` `count[i] ``=``=` `0``:` `            ``return` `False` `        ``else``:` `                ``# If element is found, decrement` `                ``# its value in the dictionary` `            ``count[i] ``-``=` `1` `    ``# Return true if both arr1 and` `    ``# arr2 are equal` `    ``return` `True`     `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `    ``arr1 ``=` `[``3``, ``5``, ``2``, ``5``, ``2``]` `    ``arr2 ``=` `[``2``, ``3``, ``5``, ``5``, ``2``]`   `    ``if` `is_arr_equal(arr1, arr2):` `        ``print``(``"Yes"``)` `    ``else``:` `        ``print``(``"No"``)`

## 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's code` `    ``public` `static` `void` `Main(String[] args)` `    ``{` `        ``int``[] arr1 = { 3, 5, 2, 5, 2 };` `        ``int``[] arr2 = { 2, 3, 5, 5, 2 };`   `        ``// Function call` `        ``if` `(areEqual(arr1, arr2))` `            ``Console.WriteLine(``"Yes"``);` `        ``else` `            ``Console.WriteLine(``"No"``);` `    ``}` `}`   `/* This code contributed by PrinciRaj1992 */`

## Javascript

 ``

Output

`Yes`

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

## Check if two arrays are equal or not using Map

• Initialise a map say unmap
• Insert all elements of array A into map
• Remove all elements of array B from map
• Check if the size of unmap becomes zero
• If zero, return true
• Otherwise, return false

Below is the implementation of the above approach:

## C++

 `// CPP program to find Non-overlapping sum` `#include ` `using` `namespace` `std;`   `// function for calculating` `// Non-overlapping sum of two array` `bool` `isEqual(vector<``int``> A, vector<``int``> B)` `{`   `    ``unordered_map<``int``, ``int``> unmap;`   `    ``// Insert all element of A into map` `    ``for` `(``auto` `i : A)` `        ``unmap[i]++;`   `    ``// Remove all elements of B from map` `    ``for` `(``auto` `i : B) {` `        ``unmap[i]--;` `        ``if` `(unmap[i] == 0)` `            ``unmap.erase(i);` `    ``}`   `    ``// Check if size of unmap becomes zero` `    ``// If zero, return true` `    ``// Otherwise, return false` `    ``if` `(unmap.size() == 0)` `        ``return` `true``;` `    ``return` `false``;` `}`   `// Driver code` `int` `main()` `{` `    ``vector<``int``> A = { 5, 4, 9, 2, 3 };` `    ``vector<``int``> B = { 5, 4, 9, 2, 3 };`   `    ``// function call` `    ``cout << isEqual(A, B);` `    ``return` `0;` `}`   `// This code is contributed by hkdass001`

## Java

 `// Java program to find Non-overlapping sum` `import` `java.util.*;`   `public` `class` `GFG {`   `  ``// function for calculating` `  ``// Non-overlapping sum of two array` `  ``static` `boolean` `isEqual(``int``[] A, ``int``[] B)` `  ``{`   `    ``HashMap unmap = ``new` `HashMap<>();`   `    ``// Insert all element of A into map` `    ``for` `(``int` `i : A)` `      ``unmap.put(i, unmap.getOrDefault(i, ``0``) + ``1``);`   `    ``// Remove all elements of B from map` `    ``for` `(``int` `i : B) {` `      ``if` `(unmap.containsKey(i)) {` `        ``unmap.put(i, unmap.get(i) - ``1``);` `        ``if` `(unmap.get(i) == ``0``)` `          ``unmap.remove(i);` `      ``}` `    ``}`   `    ``// Check if size of unmap becomes zero` `    ``// If zero, return true` `    ``// Otherwise, return false` `    ``if` `(unmap.size() == ``0``)` `      ``return` `true``;` `    ``return` `false``;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``int``[] A = { ``5``, ``4``, ``9``, ``2``, ``3` `};` `    ``int``[] B = { ``5``, ``4``, ``9``, ``2``, ``3` `};`   `    ``// function call` `    ``System.out.println((isEqual(A, B) ? ``"1"` `: ``"0"``));` `  ``}` `}`   `// This code is contributed by Karandeep1234`

## Python3

 `# Python program to find Non-overlapping sum` `from` `collections ``import` `Counter`   `# function for calculating` `# Non-overlapping sum of two array` `def` `isEqual(A, B):` `    ``# Use Counter to count the frequency of elements` `    ``unmap ``=` `Counter(A)` `    `  `    ``# Remove all elements of B from map` `    ``for` `i ``in` `B:` `        ``unmap[i] ``-``=` `1` `        ``if` `unmap[i] ``=``=` `0``:` `            ``del` `unmap[i]` `    `  `    ``# Check if size of unmap becomes zero` `    ``# If zero, return true` `    ``# Otherwise, return false` `    ``if` `len``(unmap) ``=``=` `0``:` `        ``return` `True` `    ``return` `False`   `# Driver code` `A ``=` `[``5``, ``4``, ``9``, ``2``, ``3``]` `B ``=` `[``5``, ``4``, ``9``, ``2``, ``3``]`   `# function call` `#rint(isEqual(A, B))` `print``(``1` `if` `isEqual(A, B) ``else` `0``)`

## C#

 `// C# program to find Non-overlapping sum` `using` `System;` `using` `System.Collections.Generic;`   `class` `Program {` `    ``static` `bool` `IsEqual(List<``int``> A, List<``int``> B)` `    ``{` `        ``Dictionary<``int``, ``int``> unmap` `            ``= ``new` `Dictionary<``int``, ``int``>();`   `        ``foreach``(``int` `i ``in` `A)` `        ``{` `            ``if` `(unmap.ContainsKey(i)) {` `                ``unmap[i]++;` `            ``}` `            ``else` `{` `                ``unmap[i] = 1;` `            ``}` `        ``}`   `        ``foreach``(``int` `i ``in` `B)` `        ``{` `            ``if` `(unmap.ContainsKey(i)) {` `                ``unmap[i]--;` `                ``if` `(unmap[i] == 0) {` `                    ``unmap.Remove(i);` `                ``}` `            ``}` `        ``}`   `        ``if` `(unmap.Count == 0) {` `            ``return` `true``;` `        ``}`   `        ``return` `false``;` `    ``}`   `    ``static` `void` `Main(``string``[] args)` `    ``{` `        ``List<``int``> A = ``new` `List<``int``>{ 5, 4, 9, 2, 3 };` `        ``List<``int``> B = ``new` `List<``int``>{ 5, 4, 9, 2, 3 };`   `        ``Console.WriteLine(IsEqual(A, B) ? 1 : 0);` `    ``}` `}`   `// This code is contributed by Susobhan Akhuli`

## Javascript

 `// Javascript Function` `function` `isEqual(A, B) {` `  ``let unmap = {};`   `  ``// Insert all elements of A into map` `  ``for` `(let i of A) {` `    ``unmap[i] = (unmap[i] || 0) + 1;` `  ``}`   `  ``// Remove all elements of B from map` `  ``for` `(let i of B) {` `    ``if` `(unmap[i]) {` `      ``unmap[i]--;` `      ``if` `(unmap[i] === 0) {` `        ``delete` `unmap[i];` `      ``}` `    ``}` `  ``}`   `  ``// Check if size of unmap becomes zero` `  ``// If zero, return true` `  ``// Otherwise, return false` `  ``if` `(Object.keys(unmap).length === 0) {` `    ``return` `true``;` `  ``}` `  ``return` `false``;` `}`   `// Driver Code` `let A = [5, 4, 9, 2, 3];` `let B = [5, 4, 9, 2, 3];`   `// function call` `console.log(isEqual(A, B));`   `// This code is contributed by akashish__`

Output

`1`

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

## Check if two arrays are equal or not using Counter Class

We can use the Counter class from the collections module to count the number of occurrences of each element in the arrays and then compare the resulting dictionaries.

Steps:

• Use the Counter class to count the number of occurrences of each element in a and b.
• Use the == operator to compare the resulting Counter objects.
• Return the result of the comparison.
• Import the Counter class from the collections module.

Below is the implementation of the above approach:

## C++

 `#include ` `#include ` `#include `   `bool` `arrays_equal(std::vector<``int``> a, std::vector<``int``> b) {` `    ``std::map<``int``, ``int``> map_a, map_b;` `    ``for` `(``auto` `i : a) {` `        ``map_a[i]++;` `    ``}` `    ``for` `(``auto` `i : b) {` `        ``map_b[i]++;` `    ``}` `    ``return` `map_a == map_b;` `}`   `int` `main() {` `    ``std::vector<``int``> a = {3, 2, 1, 3, 2, 1};` `    ``std::vector<``int``> b = {1, 2, 3, 1, 2, 3};` `    ``std::vector<``int``> c = {4, 5, 6, 4, 5, 6};`   `    ``std::cout << (arrays_equal(a, b) ? ``"True"` `: ``"False"``) << std::endl;` `    ``std::cout << (arrays_equal(a, c) ? ``"True"` `: ``"False"``) << std::endl;` `    ``return` `0;` `}`

## Java

 `import` `java.util.*;`   `public` `class` `GFG {` `    ``public` `static` `boolean` `arrays_equal(``int``[] a, ``int``[] b)` `    ``{` `        ``Map aCount = ``new` `HashMap<>();` `        ``Map bCount = ``new` `HashMap<>();` `        ``for` `(``int` `i = ``0``; i < a.length; i++) {` `            ``int` `count = aCount.getOrDefault(a[i], ``0``);` `            ``aCount.put(a[i], count + ``1``);` `        ``}`   `        ``for` `(``int` `i = ``0``; i < b.length; i++) {` `            ``int` `count = bCount.getOrDefault(b[i], ``0``);` `            ``bCount.put(b[i], count + ``1``);` `        ``}`   `        ``return` `aCount.equals(bCount);` `    ``}`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int``[] a = { ``3``, ``2``, ``1``, ``3``, ``2``, ``1` `};` `        ``int``[] b = { ``1``, ``2``, ``3``, ``1``, ``2``, ``3` `};` `        ``int``[] c = { ``4``, ``5``, ``6``, ``4``, ``5``, ``6` `};` `        ``System.out.println(` `            ``arrays_equal(a, b) ? ``"True"` `: ``"False"``); ``// True` `        ``System.out.println(` `            ``arrays_equal(a, c) ? ``"True"` `: ``"False"``); ``// False` `    ``}` `}`   `// This code is contributed by Susobhan Akhuli`

## Python3

 `from` `collections ``import` `Counter`   `def` `arrays_equal(a, b):` `    ``return` `Counter(a) ``=``=` `Counter(b)`   `a ``=` `[``3``, ``2``, ``1``, ``3``, ``2``, ``1``]` `b ``=` `[``1``, ``2``, ``3``, ``1``, ``2``, ``3``]` `c ``=` `[``4``, ``5``, ``6``, ``4``, ``5``, ``6``]`   `print``(arrays_equal(a, b))  ``# True` `print``(arrays_equal(a, c))  ``# False`   `# This code is contributed by Susobhan Akhuli`

## C#

 `// C# implemenatation of above approach.`   `using` `System;` `using` `System.Collections.Generic;` `using` `System.Linq;`   `public` `class` `gfg` `{` `    ``// Method to check if two arrays have the same set of elements, ignoring order` `    ``static` `bool` `ArraysEqual(List<``int``> a, List<``int``> b)` `    ``{` `        ``// Sort both arrays and compare them using the SequenceEqual method` `        ``return` `a.OrderBy(x => x).SequenceEqual(b.OrderBy(x => x));` `    ``}`   `    ``// Main method to test the ArraysEqual method` `    ``public` `static` `void` `Main()` `    ``{` `        ``// Initialize three lists of integers` `        ``List<``int``> a = ``new` `List<``int``> { 3, 2, 1, 3, 2, 1 };` `        ``List<``int``> b = ``new` `List<``int``> { 1, 2, 3, 1, 2, 3 };` `        ``List<``int``> c = ``new` `List<``int``> { 4, 5, 6, 4, 5, 6 };`   `        ``// Test the ArraysEqual method with the three lists` `        ``Console.WriteLine(ArraysEqual(a, b) ? ``"True"` `: ``"False"``);` `        ``Console.WriteLine(ArraysEqual(a, c) ? ``"True"` `: ``"False"``);` `    ``}` `}`   `// This code is contributed by Amit Mangal.`

## Javascript

 `// JavaScript implemenatation of above approach.`   `// Function to check if two arrays have the same set of elements, ignoring order` `function` `arraysEqual(a, b) {`   `  ``// Sort the arrays and convert them to strings, then compare the strings` `  ``return` `JSON.stringify(a.slice().sort()) === JSON.stringify(b.slice().sort());` `}`   `// Declare three arrays` `const a = [3, 2, 1, 3, 2, 1];` `const b = [1, 2, 3, 1, 2, 3];` `const c = [4, 5, 6, 4, 5, 6];`   `// Output the result of arraysEqual with the two pairs of arrays` `console.log(arraysEqual(a, b) ? ``"True"` `: ``"False"``);` `console.log(arraysEqual(a, c) ? ``"True"` `: ``"False"``);`     `// This code is contributed by Amit Mangal.`

Output

```True
False```

Time Complexity: O(N) [To count the number of occurrences of each element in the arrays]
Auxiliary Space: O(1)

## Check if two arrays are equal or not using using Priority Queue

We can create a custom function to compare two arrays using priority queues.

Steps:

•  First inserting all elements of both the arrays into their respective priority queues
• Then looping through them and comparing each element in the arrays.
• If all elements are equal, then return true means, the two arrays are equal.
• Otherwise return false.

Below is the implementation of the above approach:

## C++

 `// C++ program to find given two array` `// are equal or not` `#include ` `using` `namespace` `std;`   `// Function to check if two arrays are equal or not.` `bool` `arrays_equal(vector<``int``> A, vector<``int``> B, ``int` `N,` `                  ``int` `M)` `{` `    ``// If lengths of array are not equal means` `    ``// array are not equal` `    ``if` `(N != M)` `        ``return` `false``;`   `    ``// Creating 2 priority queues for 2 arrays` `    ``priority_queue<``int``> pq1;` `    ``priority_queue<``int``> pq2;`   `    ``// Insert elements of both the arrays into their` `    ``// respective priority queues` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``pq1.push(A[i]);` `        ``pq2.push(B[i]);` `    ``}`   `    ``// Traverse and check if the top elements of both the` `    ``// priority queues are same or not` `    ``while` `(N--) {` `        ``// If top elements are not same then return false` `        ``if` `(pq1.top() != pq2.top())` `            ``return` `false``;`   `        ``// If top elements are same then` `        ``// Popping top elements of both the priority queues` `        ``pq1.pop();` `        ``pq2.pop();` `    ``}` `    ``// If all elements were same.` `    ``return` `true``;` `}`   `// Driver's Code` `int` `main()` `{` `    ``vector<``int``> arr1 = { 3, 2, 1, 3, 2, 1 };` `    ``vector<``int``> arr2 = { 1, 2, 3, 1, 2, 3 };` `    ``vector<``int``> arr3 = { 4, 5, 6, 4, 5, 6 };` `    ``int` `M = ``sizeof``(arr1) / ``sizeof``(``int``);` `    ``int` `N = ``sizeof``(arr2) / ``sizeof``(``int``);` `    ``int` `L = ``sizeof``(arr3) / ``sizeof``(``int``);`   `    ``cout << (arrays_equal(arr1, arr2, M, N) ? ``"True"` `                                            ``: ``"False"``)` `         ``<< endl;` `    ``cout << (arrays_equal(arr1, arr3, M, L) ? ``"True"` `                                            ``: ``"False"``)` `         ``<< endl;` `    ``return` `0;` `}`   `// This code is contributed by Susobhan Akhuli`

## Python3

 `import` `heapq`     `def` `arrays_equal(A, B):` `    ``# If lengths of array are not equal means array are not equal` `    ``if` `len``(A) !``=` `len``(B):` `        ``return` `False`   `    ``# Creating 2 heaps for 2 arrays` `    ``heap1 ``=` `list``(A)` `    ``heap2 ``=` `list``(B)`   `    ``# Convert list into heap` `    ``heapq.heapify(heap1)` `    ``heapq.heapify(heap2)`   `    ``# Traverse and check if the top elements of both the` `    ``# heaps are same or not` `    ``while` `heap1:` `        ``# If top elements are not same then return false` `        ``if` `heapq.heappop(heap1) !``=` `heapq.heappop(heap2):` `            ``return` `False`   `    ``# If all elements were same.` `    ``return` `True`     `# Example usage` `arr1 ``=` `[``3``, ``2``, ``1``, ``3``, ``2``, ``1``]` `arr2 ``=` `[``1``, ``2``, ``3``, ``1``, ``2``, ``3``]` `arr3 ``=` `[``4``, ``5``, ``6``, ``4``, ``5``, ``6``]`   `print``(arrays_equal(arr1, arr2))  ``# Output: True` `print``(arrays_equal(arr1, arr3))  ``# Output: False`

## Java

 `import` `java.util.*;`   `public` `class` `ArraysEqual {` `    ``// Function to check if two arrays are equal or not.` `    ``public` `static` `boolean` `arraysEqual(``int``[] A, ``int``[] B,` `                                      ``int` `N, ``int` `M)` `    ``{` `        ``// If lengths of array are not equal means` `        ``// array are not equal` `        ``if` `(N != M)` `            ``return` `false``;`   `        ``// Creating 2 priority queues for 2 arrays` `        ``PriorityQueue pq1` `            ``= ``new` `PriorityQueue(` `                ``Collections.reverseOrder());` `        ``PriorityQueue pq2` `            ``= ``new` `PriorityQueue(` `                ``Collections.reverseOrder());`   `        ``// Insert elements of both the arrays into their` `        ``// respective priority queues` `        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``pq1.offer(A[i]);` `            ``pq2.offer(B[i]);` `        ``}`   `        ``// Traverse and check if the top elements of both` `        ``// the priority queues are same or not` `        ``while` `(N-- > ``0``) {` `            ``// If top elements are not same then return` `            ``// false` `            ``if` `(!pq1.peek().equals(pq2.peek()))` `                ``return` `false``;`   `            ``// If top elements are same then` `            ``// Popping top elements of both the priority` `            ``// queues` `            ``pq1.poll();` `            ``pq2.poll();` `        ``}` `        ``// If all elements were same.` `        ``return` `true``;` `    ``}`   `    ``// Driver's Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int``[] arr1 = { ``3``, ``2``, ``1``, ``3``, ``2``, ``1` `};` `        ``int``[] arr2 = { ``1``, ``2``, ``3``, ``1``, ``2``, ``3` `};` `        ``int``[] arr3 = { ``4``, ``5``, ``6``, ``4``, ``5``, ``6` `};` `        ``int` `M = arr1.length;` `        ``int` `N = arr2.length;` `        ``int` `L = arr3.length;`   `        ``System.out.println(arraysEqual(arr1, arr2, M, N)` `                               ``? ``"True"` `                               ``: ``"False"``);` `        ``System.out.println(arraysEqual(arr1, arr3, M, L)` `                               ``? ``"True"` `                               ``: ``"False"``);` `    ``}` `}`

## Javascript

 `function` `arraysEqual(A, B) {` `  ``// If lengths of array are not equal means array are not equal` `  ``if` `(A.length !== B.length) {` `    ``return` `false``;` `  ``}`   `  ``// Creating 2 heaps for 2 arrays` `  ``const heap1 = [...A];` `  ``const heap2 = [...B];`   `  ``// Convert list into heap` `  ``heap1.sort((a, b) => a - b);` `  ``heap2.sort((a, b) => a - b);`   `  ``// Traverse and check if the top elements of both the` `  ``// heaps are same or not` `  ``while` `(heap1.length > 0) {` `    ``// If top elements are not same then return false` `    ``if` `(heap1.shift() !== heap2.shift()) {` `      ``return` `false``;` `    ``}` `  ``}`   `  ``// If all elements were same.` `  ``return` `true``;` `}`   `// Example usage` `const arr1 = [3, 2, 1, 3, 2, 1];` `const arr2 = [1, 2, 3, 1, 2, 3];` `const arr3 = [4, 5, 6, 4, 5, 6];`   `console.log(arraysEqual(arr1, arr2)); ``// Output: true` `console.log(arraysEqual(arr1, arr3)); ``// Output: false`

## C#

 `// GFG` `// C# code for the given approach` `using` `System;` `using` `System.Linq;`   `class` `Program {` `    ``static` `bool` `ArraysEqual(``int``[] A, ``int``[] B)` `    ``{` `        ``// If lengths of array are not equal means` `        ``// array are not equal` `        ``if` `(A.Length != B.Length)` `            ``return` `false``;`   `        ``// Sort the two input arrays` `        ``Array.Sort(A);` `        ``Array.Sort(B);`   `        ``// Traverse both arrays and check if their elements` `        ``// are equal` `        ``for` `(``int` `i = 0; i < A.Length; i++) {` `            ``if` `(A[i] != B[i])` `                ``return` `false``;` `        ``}`   `        ``// If all elements are equal` `        ``return` `true``;` `    ``}`   `    ``static` `void` `Main(``string``[] args)` `    ``{` `        ``int``[] arr1 = { 3, 2, 1, 3, 2, 1 };` `        ``int``[] arr2 = { 1, 2, 3, 1, 2, 3 };` `        ``int``[] arr3 = { 4, 5, 6, 4, 5, 6 };`   `        ``Console.WriteLine(ArraysEqual(arr1, arr2)` `                              ``? ``"True"` `                              ``: ``"False"``); ``// True` `        ``Console.WriteLine(ArraysEqual(arr1, arr3)` `                              ``? ``"True"` `                              ``: ``"False"``); ``// False` `    ``}` `}`   `// This code is written by Sundaram`

Output

```True
False```

Time Complexity: O(N), where N is the size of the arrays.
Auxiliary Space: O(N) [To store elements into priority queues]

My Personal Notes arrow_drop_up