# Check if two arrays are permutations of each other

Given two unsorted arrays of same size, write a function that returns true if two arrays are permutations of each other, otherwise false.

Examples:

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

Input: arr1[] = {2, 1, 3, 5,}
arr2[] = {3, 2, 2, 4}
Output: No
```

We strongly recommend you to minimize your browser and try this yourself first.

A Simple Solution is sort both arrays and compare sorted arrays. Time complexity of this solution is O(nLogn)

A Better Solution is to use Hashing.
1) Create a Hash Map for all the elements of arr1[] such that array elements are keys and their counts are values.
2) Traverse arr2[] and search for each element of arr2[] in the Hash Map. If element is found then decrement its count in hash map. If not found, then return false.
3) If all elements are found then return true.

Below is the implementation of this approach.

## Java

 `// A Java program to find one array is permutation of other array ` `import` `java.util.HashMap; ` ` `  `class` `Permutaions ` `{ ` `    ``// Returns true if arr1[] and arr2[] are permutations of each other ` `    ``static` `Boolean arePermutations(``int` `arr1[], ``int` `arr2[]) ` `    ``{ ` `        ``// Creates an empty hashMap hM ` `        ``HashMap hM = ``new` `HashMap(); ` ` `  `        ``// Traverse through the first array and add elements to hash map ` `        ``for` `(``int` `i = ``0``; i < arr1.length; i++) ` `        ``{ ` `            ``int` `x = arr1[i]; ` `            ``if` `(hM.get(x) == ``null``) ` `                ``hM.put(x, ``1``); ` `            ``else` `            ``{ ` `                ``int` `k = hM.get(x); ` `                ``hM.put(x, k+``1``); ` `            ``} ` `        ``} ` ` `  `        ``// Traverse through second array and check if every element is ` `        ``// present in hash map ` `        ``for` `(``int` `i = ``0``; i < arr2.length; i++) ` `        ``{ ` `            ``int` `x = arr2[i]; ` ` `  `            ``// If element is not present in hash map or element ` `            ``// is not present less number of times ` `            ``if` `(hM.get(x) == ``null` `|| hM.get(x) == ``0``) ` `                ``return` `false``; ` ` `  `            ``int` `k = hM.get(x); ` `            ``hM.put(x, k-``1``); ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver function to test above function ` `    ``public` `static` `void` `main(String arg[]) ` `    ``{ ` `        ``int` `arr1[] = {``2``, ``1``, ``3``, ``5``, ``4``, ``3``, ``2``}; ` `        ``int` `arr2[] = {``3``, ``2``, ``2``, ``4``, ``5``, ``3``, ``1``}; ` `        ``if` `(arePermutations(arr1, arr2)) ` `            ``System.out.println(``"Arrays are permutations of each other"``); ` `        ``else` `            ``System.out.println(``"Arrays are NOT permutations of each other"``); ` `    ``} ` `} `

## C#

 `// C# program to find one array  ` `// is permutation of other array ` `using` `System; ` `using` `System.Collections.Generic;  ` `     `  `public` `class` `Permutaions ` `{ ` `    ``// Returns true if arr1[] and arr2[]  ` `    ``// are permutations of each other ` `    ``static` `Boolean arePermutations(``int` `[]arr1, ``int` `[]arr2) ` `    ``{ ` `        ``// Creates an empty hashMap hM ` `        ``Dictionary<``int``, ``int``> hM = ``new` `Dictionary<``int``, ``int``>(); ` ` `  `        ``// Traverse through the first array ` `        ``// and add elements to hash map ` `        ``for` `(``int` `i = 0; i < arr1.Length; i++) ` `        ``{ ` `            ``int` `x = arr1[i]; ` `            ``if` `(!hM.ContainsKey(x)) ` `                ``hM.Add(x, 1); ` `            ``else` `            ``{ ` `                ``int` `k = hM[x]; ` `                ``hM.Remove(x); ` `                ``hM.Add(x, k+1); ` `            ``} ` `        ``} ` ` `  `        ``// Traverse through second array and check if every element is ` `        ``// present in hash map ` `        ``for` `(``int` `i = 0; i < arr2.Length; i++) ` `        ``{ ` `            ``int` `x = arr2[i]; ` ` `  `            ``// If element is not present in hash map or element ` `            ``// is not present less number of times ` `            ``if` `(!hM.ContainsKey(x)) ` `                ``return` `false``; ` ` `  `            ``int` `k = hM[x]; ` `            ``if` `(!hM.ContainsKey(x)) ` `                ``hM.Add(x, k-1); ` `            ``else``{ ` `                ``int` `a = hM[x]; ` `                ``hM.Remove(x); ` `                ``hM.Add(x, a+1); ` `            ``} ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `[]arr1 = {2, 1, 3, 5, 4, 3, 2}; ` `        ``int` `[]arr2 = {3, 2, 2, 4, 5, 3, 1}; ` `        ``if` `(arePermutations(arr1, arr2)) ` `            ``Console.WriteLine(``"Arrays are permutations of each other"``); ` `        ``else` `            ``Console.WriteLine(``"Arrays are NOT permutations of each other"``); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

`Arrays are permutations of each other`

Time complexity of this method is O(n) under the assumption that we have a hash function inserts and finds elements in O(1) time.